forked from nd0ut/semantic-ui-rails
-
Notifications
You must be signed in to change notification settings - Fork 1
/
semantic.thor
168 lines (123 loc) · 4.26 KB
/
semantic.thor
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
require 'pry'
require 'fileutils'
require 'git'
require 'logger'
require 'json'
class Semantic < Thor
include Thor::Actions
REPO_URI = "https://github.com/jlukic/Semantic-UI.git"
desc "update", "fetch Semantic UI code from git"
method_option :branch, default: "master"
def update
if File.directory? working_dir
say_status "MESSAGE", "WORKING DIR EXIST"
pull
else
say_status "MESSAGE", "THERE IS NO WORKING DIR"
prepare
clone
end
parse_version
copy_files
fix_paths
generate_templates
end
no_commands do
def clone
say_status "STEP", "CLONE REPO"
Dir.chdir working_dir
git = Git.clone(REPO_URI, 'semantic-ui')
end
def pull
begin
say_status "STEP", "PULL REPO"
git = Git.open(git_root, :log => Logger.new(STDOUT))
git.pull
rescue
puts "no internet connection"
end
end
def parse_version
say_status "STEP", "PARSE VERSION"
Dir.chdir git_root
bower = JSON.parse( IO.read('composer.json'), :quirks_mode => true)
version = bower["version"]
version_file = source_root + "lib/semantic/ui/rails/version.rb"
gsub_file version_file, /(?<=VERSION = \")(.+)(?=\")/, version
end
def fix_paths
Dir.glob(source_root + "vendor" + "**/*.less") do |file|
gsub_file file, /(?<=url\()(.+\/\w+)(?=\/)/, '..'
end
end
def copy_files
say_status "STEP", "COPY FILES"
# STYLESHEETS
stylesheets_path = "vendor/assets/stylesheets/semantic-ui/"
run "rsync -avm --include='*.less' --include='*.css' -f 'hide,! */' #{git_root + 'src/'} #{source_root + stylesheets_path}"
# JAVASCRIPTS
javascripts_path = "vendor/assets/javascripts/semantic-ui/"
run "rsync -avm --include='*.js' -f 'hide,! */' #{git_root + 'src/'} #{source_root + javascripts_path}"
# FONTS
fonts_path = "vendor/assets/fonts/semantic-ui/"
run "rsync -avm --include='*.*' -f 'hide,! */' #{git_root + 'src/fonts/'} #{source_root + fonts_path}"
# IMAGES
images_path = "vendor/assets/images/semantic-ui/"
run "rsync -avm --include='*.*' -f 'hide,! */' #{git_root + 'src/images/'} #{source_root + images_path}"
end
def generate_templates
# JAVASCRIPTS
say_status "STEP", "GENERATE JAVASCRIPT TEMPLATE"
js_template_path = source_root + "lib/generators/semantic/install/templates/semantic-ui.js"
javascripts_path = Pathname.new(source_root + "vendor/assets/javascripts/semantic-ui")
FileUtils.rm js_template_path
File.open(js_template_path, 'a') do |template|
Dir.glob(source_root + javascripts_path + "**/*") do |file|
next if File.directory? file
filepath = Pathname.new(file)
relative_path = filepath.relative_path_from(javascripts_path)
template.write "//= require semantic-ui/#{relative_path} \n"
end
end
# STYLESHEETS
say_status "STEP", "GENERATE STYLESHEETS TEMPLATE"
css_template_path = source_root + "lib/generators/semantic/install/templates/semantic-ui.css.less"
stylesheets = Pathname.new(source_root + "vendor/assets/stylesheets/semantic-ui")
FileUtils.rm css_template_path
dirs_order = %w(elements collections views modules)
File.open(css_template_path, 'a') do |template|
dirs_order.each do |dir|
template.write "/* #{dir.capitalize} */ \n"
Dir.glob(source_root + stylesheets + dir + "**/*") do |file|
next if File.directory? file
filepath = Pathname.new(file)
relative_path = filepath.relative_path_from(stylesheets)
template.write "@import 'semantic-ui/#{relative_path}'; \n"
end
template.write "\n"
end
end
end
def clean
say_status "MESSAGE", "DELETE WORKING DIR"
FileUtils.rm_rf 'tmp/updater'
end
def prepare
say_status "MESSAGE", "CREATE WORKING DIR"
FileUtils.mkdir_p 'tmp/updater'
end
# dirs
def self.source_root
File.dirname(__FILE__)
end
def git_root
working_dir + 'semantic-ui'
end
def working_dir
source_root + 'tmp/updater'
end
def source_root
Pathname.new File.dirname(__FILE__)
end
end
end