-
Notifications
You must be signed in to change notification settings - Fork 162
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of github.com:xui/xui into dev
- Loading branch information
Showing
10 changed files
with
253 additions
and
145 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,138 @@ | ||
#!/usr/bin/env ruby | ||
require 'YAML' | ||
require 'fileutils' | ||
|
||
PROFILE_PATH = "util/profiles" | ||
|
||
# TODO: print out build command line if no args provided. | ||
# usage: ./build profile=whatever --minify | ||
|
||
# man I sure love Windows! --alunny | ||
is_windows = Dir.getwd[0,1] != "/" | ||
echo_dot = is_windows ? "echo." : "echo" | ||
mkdir = is_windows ? "mkdir" : "mkdir -p" | ||
def show_usage | ||
STDERR.puts "\nusage: ./build [ profile=<name> ] [ --minify ] [ --generate-docs ]" | ||
STDERR.puts "\nsupported profiles are:" | ||
Dir.chdir(PROFILE_PATH) do | ||
STDERR.puts Dir['*.js'].map {|profile| "\t#{profile.sub(/\.js/, '')}"} | ||
end | ||
STDERR.puts "\nIf no profile is selected, 'core' will be used\n\n" | ||
exit(1) | ||
end | ||
|
||
profile_matcher = /^profile=(.*)$/ | ||
profile_path = "util/profiles/" | ||
# defaults | ||
profile = "core" | ||
minify = false | ||
docs = false | ||
|
||
$*.each do |arg| | ||
minify = true if arg == "--minify" | ||
docs = true if arg == "--doc" | ||
profile = arg.match(profile_matcher)[1] if arg.match(profile_matcher) | ||
ARGV.each do |arg| | ||
case arg | ||
when "--minify" then minify = true | ||
when /^profile=(.*)$/ then profile = $1 | ||
when "--generate-docs" then docs = true | ||
when "--help" then show_usage | ||
else | ||
STDERR.puts("Invalid parameter '#{arg}'") unless arg == "--help" | ||
show_usage | ||
end | ||
end | ||
hash = YAML.load_file "#{ profile_path }#{ profile }.js" | ||
hash = YAML.load_file(File.join(PROFILE_PATH, profile) + ".js") | ||
|
||
# clear previous file | ||
`#{ mkdir } lib` | ||
`#{ echo_dot } > #{ hash["out"] }` | ||
target = hash["out"] | ||
target_dir = File.dirname(target) | ||
|
||
hash["include"].each do |include| | ||
# a bit of overhead since not all files end in a newline | ||
`cat #{ include }.js >> #{ hash["out"] }` | ||
`#{ echo_dot } >> #{ hash["out"] }` | ||
end | ||
Dir.mkdir(target_dir) unless File.directory?(target_dir) | ||
File.unlink(target) if File.exist?(target) | ||
|
||
content = hash["include"].map {|library| File.read(library + ".js") } | ||
|
||
File.open(target, "w") {|op| op.puts content } | ||
|
||
if minify | ||
puts "Minify-ing..." | ||
minified_filename = hash['out'].sub(/\.js$/, '.min.js') | ||
`java -jar util/compiler.jar --js=#{ hash["out"] } \ | ||
--js_output_file=#{ hash["out"] }.min` | ||
--js_output_file=#{ minified_filename }` | ||
end | ||
|
||
if docs | ||
XUI_DIR = File.expand_path(File.dirname(__FILE__)) | ||
DOC_DIR = File.join(XUI_DIR, 'doc') | ||
SRC_DIR = File.join(XUI_DIR, 'src') | ||
|
||
FileUtils.rm_r(DOC_DIR) if File.exists?(DOC_DIR) | ||
FileUtils.mkdir(DOC_DIR) | ||
|
||
FileUtils.cd(SRC_DIR) do | ||
files = Dir['**/*.js'].reject{ |f| (f =~ /ie\//) != nil }.join(' ') | ||
`jodoc --output ../doc #{files}` | ||
end | ||
end | ||
|
||
# Generate HTML | ||
puts "Generating documentation as HTML..." | ||
generate_documentation(HTML_DIR) | ||
|
||
# Generate EJS | ||
puts "Generating documentation as EJS..." | ||
generate_documentation(EJS_DIR) | ||
html_to_ejs(EJS_DIR) | ||
end | ||
|
||
if docs | ||
XUI_DIR = File.expand_path(File.dirname(__FILE__)) | ||
SRC_DIR = File.join(XUI_DIR, 'src') | ||
DOC_DIR = File.join(XUI_DIR, 'doc') | ||
HTML_DIR = File.join(DOC_DIR, 'html') | ||
EJS_DIR = File.join(DOC_DIR, 'ejs') | ||
|
||
def generate_documentation(output_directory) | ||
output_directory = File.expand_path(output_directory) | ||
FileUtils.mkdir_p(output_directory) | ||
|
||
FileUtils.cd(SRC_DIR) do | ||
files = Dir['**/*.js'].reject{ |f| (f =~ /ie\//) != nil }.join(' ') | ||
`jodoc --title "XUI API Reference" --output #{output_directory} #{files}` | ||
end | ||
end | ||
|
||
def html_to_ejs(directory) | ||
FileUtils.cd(directory) do | ||
rename = Hash.new | ||
|
||
# Generate EJS filenames | ||
Dir['**/*'].each do |filename| | ||
# Remove prefix js_ | ||
# Remove prefix _ | ||
# Rename base to basics | ||
# Remove .html and .js.html | ||
# Add extension .ejs | ||
rename[filename] = filename.sub(/^js_/, '') \ | ||
.sub(/^_/, '') \ | ||
.sub(/base\./, 'basics.') \ | ||
.sub(/\..*html/, '') \ | ||
.concat('.ejs') | ||
end | ||
|
||
# Rename each file | ||
rename.each { |o, n| FileUtils.mv(o, n) } | ||
|
||
# Alter file content | ||
Dir['**/*'].each do |filename| | ||
data = File.read(filename) | ||
|
||
# Only save content of <body> | ||
data.sub!(/.*<body>(.*)<\/body>.*/mi, "\\1") | ||
# Update links to each filename | ||
rename.each { |o, n| data.gsub!(o, File.basename(n, '.ejs')) } | ||
|
||
File.open(filename, 'w') { |file| file.write(data) } | ||
end | ||
end | ||
end | ||
|
||
FileUtils.rm_r(DOC_DIR) if File.exists?(DOC_DIR) | ||
|
||
# Generate HTML | ||
generate_documentation(HTML_DIR) | ||
|
||
# Generate EJS | ||
generate_documentation(EJS_DIR) | ||
html_to_ejs(EJS_DIR) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.