-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathciPrepFunctions.rb
104 lines (85 loc) · 2.84 KB
/
ciPrepFunctions.rb
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
#!/usr/bin/env ruby
=begin
Script and functions for getting functions for ChemInfo.
- Given a directory containing JS function definition scripts
- Get all function definitions and names
- Place all in a JS script
=end
# Function definitions
# -----------------------------------------------------------------------------
# Get entries in directory by extension regex
def dirExt(dir, ext = /\.js$/)
return(Dir.entries(dir).select{|e| e.match?(ext)})
end
# Strip line and inline comments from a newline-split source file
def removeComments(text)
# Currently hardcoded for JS comments and the two-space inline convention
expr = {:line => /^(\s+)?[\/\*]/, :inline => / \/.+$/}
return(
text.reject{|t| t.match?(expr[:line])}.map{|t| t.gsub(expr[:inline], '')}
)
end
# Get the top / header line of a function definition file, and get function
# name and swapped form
def getTopLine(text)
functionName = text.match(/(?<=function )([^\(]+)/)[0]
swapped = "#{functionName}: #{text.gsub(/(?<=function) [^\(]+/, '')}"
return({:name => functionName, :swap => swapped})
end
# Operations
# -----------------------------------------------------------------------------
# Initialise function store and output string
functions = {}
output = %Q[// NOTE:
// The following section was generated by an external Ruby script.
// Caution is advised if modifying the contents.
/*** Function definitions ***/
]
# State directiry from, directory to, output file, and file extension regex
dirFrom = "./scripts"
dirTo = "./cheminfo"
outputName = "#{dirTo}/AUTO_nciFunctionDefs.js"
ext = /\.js$/
# Filenames to include
include = [
"assignEmptyheader",
"cleanNames",
"convRawTsvJson",
"getAllCompounds",
"getWithId",
"numLinks",
"pickAnchor",
"reduceShortlist",
"runSimulation",
"shortlistReactions",
"subsetData",
"swapOtherIds"
].map{|e| "#{e}.js"}
# Get the files by extension regex in dirFrom
inDir = dirExt(dirFrom, ext).select{|e| include.any?(e)}
# Map over the target files
inDir.each do |file|
# Read the file and strip comments
text = removeComments(File.read("#{dirFrom}/#{file}").split(/\n/))
# Reject empty lines
text.reject!{|e| e.empty?}
# Strip the trailing semi-colon in the last line, if present
text[text.length - 1] = text.last.gsub(/};$/, "}")
# Get header line data but don't swap around
topLine = getTopLine(text[0])
# Record in the functions hash
functions[topLine[:name].to_sym] = text
end
# Get the function names array
functionNames = functions.keys.map{|k| k.to_s}
# Map over the sorted functions hash
functions.sort.to_h.each_pair do |k, v|
# If it ends with a function definition, append a comma, newline, and indent
output += ";\n\n" if output.match?(/\}$/)
# Append function text
output += v.join("\n")
end
# Close the output
output += %Q[;\n]
# Write to the output
File.write(outputName, output, :mode => "w")