Class: Shift::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/shift-lang.rb

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ CLI

Returns a new instance of CLI.



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
# File 'lib/shift-lang.rb', line 9

def initialize(args)
	if(!args[0])
		puts "Enter the project name : "
		project_name = gets.chomp

		if(!File.exists?(project_name))
			Dir.mkdir(project_name)
			Dir.chdir(project_name)

			puts "Creating " + project_name + ".shift"
			puts "Creating views directory"
			Dir.mkdir("views")
			file = File.new(project_name + ".shift", "w")
			file.puts ""
			file.close
		else
			puts "A file already exists with the same name"
		end
	else
		case args[0]
		when "generate"
			case args[1]
			when "python"
				generate_python
			when "ruby"
				generate_ruby
			when "javascript"
				generate_javascript
			else
				puts "Unknown target platform. Options are ruby, python and javascript"
			end
		else
			puts "Unknown option"
		end
	end				
end

Instance Method Details

#generate_javascriptObject



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
# File 'lib/shift-lang.rb', line 184

def generate_javascript()
	files = get_files

	generated_code = Shift::Generator::generateJavaScript(files)

	if generated_code == ""
		return
	end

	project_name = get_project_name

	view_sources = File.absolute_path(File.join(Dir.pwd, "views"))
	
	if(!File.exists?("release"))
		Dir.mkdir("release")
	end
	Dir.chdir("release")

	if(File.exists?("javascript"))
		FileUtils.remove_dir("javascript", true)
	end

	Dir.mkdir("javascript")
	Dir.chdir("javascript")			

	puts "Creating target folder 'release/" + project_name + "'"
	Dir.mkdir(project_name)
	Dir.chdir(project_name)	

	puts "Writing application file"
	File.open("app.js", "w") do |file|
		file.puts generated_code
	end
	
	puts "Copying templates"
	FileUtils.cp_r(view_sources, Dir.pwd)
	
	puts "Done."	
end

#generate_pythonObject



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
# File 'lib/shift-lang.rb', line 77

def generate_python()
	files = get_files

	generated_code = Shift::Generator::generatePython(files)

	if generated_code == ""
		return
	end

	project_name = get_project_name

	view_sources = File.absolute_path(File.join(Dir.pwd, "views"))

	gae_sources = File.absolute_path(File.join(File.dirname(__FILE__), "app_engine_templates"))
	pystache = File.join(gae_sources, "pystache")
	config = File.join(gae_sources, "app.yaml")

	config_contents = File.open(config, "r") { |io| io.read }

	config_template = ERB.new(config_contents)

	if(!File.exists?("release"))
		Dir.mkdir("release")
	end
	Dir.chdir("release")

	if(File.exists?("python"))
		FileUtils.remove_dir("python", true)
	end

	Dir.mkdir("python")
	Dir.chdir("python")

	puts "Creating target folder 'release/" + project_name + "'"
	Dir.mkdir(project_name)
	Dir.chdir(project_name)	

	puts "Writing application file"
	File.open(project_name + ".py", "w") do |file|
		file.puts generated_code
	end
	
	puts "Copying templates"
	FileUtils.cp_r(pystache, Dir.pwd)
	FileUtils.cp_r(view_sources, Dir.pwd)

	template_files = get_files_from_directory(File.join(Dir.pwd, "views"), ".html", false)
	template_files.each do | template_file |
		text = File.read(template_file)
		File.open(template_file, "w") { | file | file.puts text.gsub("{{id}}", "{{#key}}{{id}}{{/key}}") }
	end

	puts "Writing Google App Engine configuration file"
	File.open("app.yaml", "w") do |file|
		file.puts config_template.result(binding)
	end
	
	puts "Done."
end

#generate_rubyObject



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
# File 'lib/shift-lang.rb', line 137

def generate_ruby()
	files = get_files

	generated_code = Shift::Generator::generateRuby(files)

	if generated_code == ""
		return
	end
	
	project_name = get_project_name

	view_sources = File.absolute_path(File.join(Dir.pwd, "views"))
	template_sources = File.absolute_path(File.join(File.dirname(__FILE__), "heroku_templates"))
	gemfile = File.join(template_sources, "Gemfile")
	procfile = File.join(template_sources, "Procfile")
	
	if(!File.exists?("release"))
		Dir.mkdir("release")
	end
	Dir.chdir("release")

	if(File.exists?("ruby"))
		FileUtils.remove_dir("ruby", true)
	end

	Dir.mkdir("ruby")
	Dir.chdir("ruby")			

	puts "Creating target folder 'release/" + project_name + "'"
	Dir.mkdir(project_name)
	Dir.chdir(project_name)	

	puts "Writing application file"
	File.open("app.rb", "w") do |file|
		file.puts generated_code
	end
	
	puts "Copying templates"
	FileUtils.cp_r(view_sources, Dir.pwd)

	puts "Generating Heroku configuration files"
	FileUtils.cp(gemfile, Dir.pwd)
	FileUtils.cp(procfile, Dir.pwd)
	
	puts "Done."
end

#get_filesObject



68
69
70
# File 'lib/shift-lang.rb', line 68

def get_files()
	get_files_from_directory Dir.pwd
end

#get_files_from_directory(directory, extension = ".shift", log = true) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/shift-lang.rb', line 46

def get_files_from_directory(directory, extension = ".shift", log = true)
	files = []
	Dir.foreach(directory) do | file | 
		if(File.directory?(file) == false)
			file = File.join(directory, file)
			if(File.extname(file) == extension)
				if log
					puts "Found file : " + File.absolute_path(file)
				end
				files.push File.absolute_path(file)
			end
		else
			if ((file.to_s != '.') && (file.to_s != '..'))
				file = File.join(directory, file)
				files.concat get_files_from_directory(file)
			end
		end
	end

	files
end

#get_project_nameObject



72
73
74
75
# File 'lib/shift-lang.rb', line 72

def get_project_name()
	project_name = Dir.pwd.split("/")
	project_name[project_name.length - 1]
end