-
Notifications
You must be signed in to change notification settings - Fork 1
/
revitAddinDisabler.js
304 lines (261 loc) · 9.87 KB
/
revitAddinDisabler.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
const fs = require('fs')
const process = require('process')
const path = require('path')
const child_process = require('child_process');
var inquirer = require('inquirer');
const glob = require("glob")
const keypress = require('keypress');
//Environment variables:
const appdata = process.env.appdata
// debug logging
const util = require('util');
const debugapp = util.debuglog('debugapp');
const debugstart = util.debuglog('debugstart');
/* -------------------------------------------------------------------------- */
/* FUNCTIONS */
/* -------------------------------------------------------------------------- */
function getRevitVersions() {
//find installed revit verions from the registry
return new Promise((resolve, reject) => {
//get installed programs
var installedPrograms = child_process.spawnSync('reg', ['query HKLM\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall /s'], {
shell: true
}).output.toString().split("\r\n\r\n")
var revitversions = []
debugstart(installedPrograms)
//find revit
for (let i = 0; i < installedPrograms.length; i++) {
const programRegistry = installedPrograms[i].split("\r\n")
// debugapp('Checking program:', programRegistry[0])
//find DisplayName
for (let j = 0; j < programRegistry.length; j++) {
const registryValue = programRegistry[j];
if (registryValue.indexOf('DisplayName') > 0) {
//Find program
const programNameValue = registryValue.split(' ')
const programName = programNameValue[programNameValue.length - 1]
debugapp('program name:', programName)
// Check if it's Revit
const revitRegex = new RegExp('^ *Revit \\d{4}$');
debugapp(revitRegex.test(programName))
if (revitRegex.test(programName)) {
const programNameArray = programName.split(' ')
revitversions.push(programNameArray[programNameArray.length - 1])
}
}
}
}
revitversions.sort()
resolve(revitversions);
})
}
function findAddinFiles(version) {
//find addin files and print nice list
//calls selectoptions
var addinpath = []
var addinFiles = []
//create addin folder's paths
addinpath[0] = path.join(appdata, '\\Autodesk\\Revit\\Addins', version)
addinpath[1] = path.join('C:\\ProgramData\\Autodesk\\Revit\\Addins', version)
//find addin files in addinpaths
for (let i = 0; i < addinpath.length; i++) {
const element = addinpath[i];
var f = glob.sync(element + '/*.addin*')
addinFiles = addinFiles.concat(f)
}
// find version specific addins in ApplicationPlugins
var addinRoot = 'C:\\ProgramData\\Autodesk\\ApplicationPlugins\\'
var g = glob.sync(addinRoot + '/**/' + version + '/**/*.addin*')
addinFiles = addinFiles.concat(g)
//find not version specific plugins in ApplicationPlugins
var h = glob.sync(addinRoot + '/**/!(20)??/*.addin*')
addinFiles = addinFiles.concat(h)
//find addins in program files subfolders
var programfilesAddins = 'C:\\Program Files\\Autodesk\\Revit ' + version + '\\AddIns\\'
var i = glob.sync(programfilesAddins + '/**/*.addin*')
addinFiles = addinFiles.concat(i)
//create lists
var addinList = [] //all data, it will be converted to objects after sort
var addinNames = [] //only names, used for finding longest only
for (let i = 0; i < addinFiles.length; i++) {
var pathArray = addinFiles[i].split("/")
var fileName = pathArray[pathArray.length - 1]
var addinName = fileName.split(".addin")
var addinStatus
if (addinName[1] == ".disabled") {
addinStatus = "disabled"
} else {
addinStatus = "enabled"
}
var line = addinName[0] + "|" + addinStatus + "|" + addinFiles[i]
addinList.push(line)
addinNames.push(addinName[0])
}
//sort addins
addinList.sort()
addinNames.sort()
//find longest addin name
var longestline = 1
for (let i = 0; i < addinNames.length; i++) {
const element = addinNames[i];
if (addinNames[i].length > longestline) {
longestline = addinNames[i].length
}
}
var addinObjects = []
var addinTable = []
//rebuild lists and objects sorted
for (let i = 0; i < addinList.length; i++) {
var currarr = addinList[i].split("|")
var obj = {
name: currarr[0],
status: currarr[1],
path: currarr[2]
}
addinObjects.push(obj)
var cname = {
name: addinNames[i].padEnd(longestline + 2) + currarr[1],
value: i
}
addinTable.push(cname)
}
selectoption(version, addinTable, addinObjects)
}
/**
*
* @param {String} version Revit verision
* @param {Array} tabl formatted names statuses and numbers
* @param {Array} data all data in an array of objects
* @param {Boolean} pathvis path is visible
*/
function selectoption(version, tabl, data, pathvis = false) {
var pluschoices = [
new inquirer.Separator(),
{
name: 'Enable all',
value: 'enabled'
},
{
name: 'Disable all',
value: 'disabled'
},
{
name: 'Show/hide paths',
value: 'showpath'
},
{
name: 'Exit',
value: 'exit'
},
new inquirer.Separator()
]
let tablExt = pluschoices.concat(tabl)
inquirer.prompt([{
type: 'list',
name: 'addin',
message: 'Select addin to disable',
pageSize: process.stdout.rows - 3,
choices: tablExt
}])
.then(answers => {
// console.log(answers.addin)
switch (answers.addin) {
case 'exit':
console.log()
console.log('Thanks for using this program!')
console.log('Please restart Revit if it\'s running')
console.log()
console.log('Press any key to exit!')
keypress(process.stdin)
process.stdin.on('keypress', _ => {
process.stdin.pause()
});
process.stdin.setRawMode(true);
process.stdin.resume();
break;
case 'enabled':
case 'disabled':
for (let i = 0; i < data.length; i++) {
var last = false
if (i + 1 == data.length) {
last = true
}
chAddin(version, i, data, answers.addin, last)
}
break;
case 'showpath':
if (pathvis) {
for (let i = 0; i < tabl.length; i++) {
tabl[i].name = tabl[i].origname
}
selectoption(version, tabl, data, false)
} else {
for (let i = 0; i < tabl.length; i++) {
tabl[i].origname = tabl[i].name
tabl[i].name = tabl[i].name + " " + data[i].path
}
selectoption(version, tabl, data, true)
}
break;
default:
chAddin(version, answers.addin, data, false, false)
break;
}
})
}
/**
* change addin to disabled or enabled
* @param {string} version Revit version
* @param {number} addinnr Number of addin in the list
* @param {Array} fullobj All addins as objects
* @param {*} forcedStatus "enabled"|"disabled"|false: force enable/disable all
* @param {boolean} last Last element in enable/disable all
*/
function chAddin(version, addinnr, fullobj, forcedStatus, last) {
var currObj = fullobj[addinnr]
var np
var targetStatus
if ((currObj.status == "enabled" && !forcedStatus) || (currObj.status == "enabled" && forcedStatus == "disabled")) {
np = currObj.path + ".disabled"
targetStatus = "Disabled"
} else if ((currObj.status == "disabled" && !forcedStatus) || (currObj.status == "disabled" && forcedStatus == "enabled")) {
np = currObj.path.replace('.disabled', '')
targetStatus = "Enabled"
} else if (last) {
findAddinFiles(version)
} else {
return
}
var oldPath = path.join(currObj.path)
var newPath = path.join(np)
fs.rename(oldPath, newPath, (err) => {
if (err) throw err;
console.log()
console.log(targetStatus, currObj.name)
if (!forcedStatus || last) {
console.log()
findAddinFiles(version)
}
})
}
/* -------------------------------------------------------------------------- */
/* START PROMPTS */
/* -------------------------------------------------------------------------- */
getRevitVersions()
.then(rv => {
console.log("=== revitAddinDisabler ===")
console.log()
console.log("In case of an error try running this program as Administrator!")
console.log("Restart Revit after running this program! Or close it now.")
console.log()
//ask for revit version
return inquirer.prompt([{
type: 'list',
name: 'version',
message: 'Revit version:',
choices: rv
}])
})
.then(answers => {
findAddinFiles(answers.version)
})