terrimporter 0.4.0 → 0.5.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +1 -0
- data/Gemfile.lock +6 -9
- data/Rakefile +69 -0
- data/bin/terrimporter +5 -1
- data/config/schema.yml +0 -3
- data/config/terrimporter.config.yml +4 -7
- data/lib/{app_logger.rb → terrimporter/app_logger.rb} +0 -0
- data/lib/terrimporter/config_helper.rb +46 -0
- data/lib/{config_validator.rb → terrimporter/config_validator.rb} +0 -0
- data/lib/terrimporter/configuration.rb +72 -0
- data/lib/terrimporter/downloader.rb +38 -0
- data/lib/terrimporter/importer.rb +185 -0
- data/lib/{options.rb → terrimporter/options.rb} +16 -8
- data/lib/terrimporter/version.rb +3 -2
- data/lib/terrimporter.rb +30 -26
- data/tasks/load_tasks.rb +3 -0
- data/tasks/version.rake +2 -0
- data/terrimporter.gemspec +7 -4
- data/test/fixtures/css/base.css +1 -0
- data/test/fixtures/css/ie.css +1 -0
- data/test/fixtures/html/img_backgrounds_dir.html +43 -0
- data/test/fixtures/html/img_dir.html +67 -0
- data/test/fixtures/html/js_dyn_dir.html +44 -0
- data/test/fixtures/img/background.jpg +0 -0
- data/test/fixtures/img/testimage.png +0 -0
- data/test/fixtures/invalid.config.yml +27 -0
- data/test/fixtures/js/base.js +1 -0
- data/test/fixtures/js/dynlib.js +1 -0
- data/test/fixtures/test.config.yml +64 -0
- data/test/helper.rb +0 -19
- data/test/terrimporter.config.yml +68 -0
- data/test/test/tmp/public/javascripts/base.js +1 -0
- data/test/test/tmp/public/javascripts/lib/dynlib.js +1 -0
- data/test/test/tmp/public/stylesheets/base.css +1 -0
- data/test/test/tmp/public/stylesheets/ie.css +1 -0
- data/test/test_helper.rb +47 -0
- data/test/test_terrimporter.rb +66 -4
- data/test/unit/test_app_logger.rb +51 -0
- data/test/unit/test_config_helper.rb +33 -0
- data/test/unit/test_config_validator.rb +20 -0
- data/test/unit/test_configuration.rb +30 -26
- data/test/unit/test_downloader.rb +38 -0
- data/test/unit/test_importer.rb +159 -23
- data/test/unit/test_options.rb +45 -7
- metadata +80 -24
- data/lib/configuration.rb +0 -93
- data/lib/importer.rb +0 -190
- data/public/javascripts/base.js +0 -0
- data/public/javascripts/pngfix.js +0 -0
- data/public/javascripts/warning.js +0 -0
- data/test/unit/test_terrimporter.rb +0 -23
data/lib/configuration.rb
DELETED
@@ -1,93 +0,0 @@
|
|
1
|
-
require 'etc'
|
2
|
-
require 'kwalify'
|
3
|
-
require 'config_validator'
|
4
|
-
|
5
|
-
module TerrImporter
|
6
|
-
class Application
|
7
|
-
class Configuration < Hash
|
8
|
-
|
9
|
-
CONFIG_DEFAULT_NAME = 'terrimporter.config.yml'
|
10
|
-
SCHEMA_DEFAULT_NAME = 'schema.yml'
|
11
|
-
|
12
|
-
attr_accessor :validations, :config_file
|
13
|
-
|
14
|
-
def initialize(config_file = nil)
|
15
|
-
self.config_file = config_file unless config_file.nil?
|
16
|
-
|
17
|
-
end
|
18
|
-
|
19
|
-
def load_configuration
|
20
|
-
config_file_path = determine_config_file_path
|
21
|
-
validate_and_load_config(config_file_path)
|
22
|
-
end
|
23
|
-
|
24
|
-
def determine_config_file_path
|
25
|
-
unless self.config_file.nil?
|
26
|
-
return self.config_file
|
27
|
-
end
|
28
|
-
|
29
|
-
valid_config_paths.each do |path|
|
30
|
-
file_path = File.join path, CONFIG_DEFAULT_NAME
|
31
|
-
return file_path if File.exists?(file_path)
|
32
|
-
end
|
33
|
-
|
34
|
-
raise ConfigurationError, %Q{config file #{CONFIG_DEFAULT_NAME} not found in search paths. Search paths are:
|
35
|
-
#{valid_config_paths.join "\n"} \n If this is a new project, run with the option --init}
|
36
|
-
end
|
37
|
-
|
38
|
-
def valid_config_paths
|
39
|
-
[
|
40
|
-
Dir.pwd,
|
41
|
-
File.join(Dir.pwd, 'config'),
|
42
|
-
File.join(Dir.pwd, '.config'),
|
43
|
-
Etc.getpwuid.dir
|
44
|
-
]
|
45
|
-
end
|
46
|
-
|
47
|
-
#todo split!
|
48
|
-
def validate_and_load_config(file)
|
49
|
-
puts "Load configuration "
|
50
|
-
|
51
|
-
schema = Kwalify::Yaml.load_file(schema_file_path)
|
52
|
-
#validator = Kwalify::Validator.new(schema)
|
53
|
-
validator = ConfigValidator.new(schema)
|
54
|
-
|
55
|
-
parser = Kwalify::Yaml::Parser.new(validator)
|
56
|
-
document = parser.parse_file(file)
|
57
|
-
## show errors if exist
|
58
|
-
errors = parser.errors()
|
59
|
-
##todo convert to single statement, map for example
|
60
|
-
if errors && !errors.empty?
|
61
|
-
error_message = ""
|
62
|
-
for e in errors
|
63
|
-
error_message << "#{e.linenum}:#{e.column} [#{e.path}] #{e.message}\n"
|
64
|
-
end
|
65
|
-
raise ConfigurationError, error_message
|
66
|
-
end
|
67
|
-
|
68
|
-
self.merge! document
|
69
|
-
|
70
|
-
end
|
71
|
-
|
72
|
-
def schema_file_path
|
73
|
-
File.join(File.dirname(__FILE__), '..', 'config', SCHEMA_DEFAULT_NAME)
|
74
|
-
end
|
75
|
-
|
76
|
-
#todo
|
77
|
-
def validate_schema
|
78
|
-
meta_validator = Kwalify::MetaValidator.instance
|
79
|
-
|
80
|
-
## validate schema definition
|
81
|
-
parser = Kwalify::Yaml::Parser.new(meta_validator)
|
82
|
-
errors = parser.parse_file(schema_file_path)
|
83
|
-
for e in errors
|
84
|
-
puts "#{e.linenum}:#{e.column} [#{e.path}] #{e.message}"
|
85
|
-
end if errors && !errors.empty?
|
86
|
-
end
|
87
|
-
|
88
|
-
def create_config
|
89
|
-
FileUtils.cp(File.join(File.dirname(__FILE__), "..", "config", CONFIG_DEFAULT_NAME), File.join(Dir.pwd, CONFIG_DEFAULT_NAME))
|
90
|
-
end
|
91
|
-
end
|
92
|
-
end
|
93
|
-
end
|
data/lib/importer.rb
DELETED
@@ -1,190 +0,0 @@
|
|
1
|
-
require 'fileutils'
|
2
|
-
require 'app_logger'
|
3
|
-
require 'yaml'
|
4
|
-
require 'uri'
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
module TerrImporter
|
9
|
-
|
10
|
-
class DefaultError < StandardError
|
11
|
-
end
|
12
|
-
|
13
|
-
class ConfigurationError < StandardError
|
14
|
-
end
|
15
|
-
|
16
|
-
class Importer
|
17
|
-
require 'options'
|
18
|
-
require 'configuration'
|
19
|
-
include Logging
|
20
|
-
|
21
|
-
#todo remove
|
22
|
-
attr_accessor :options, :config
|
23
|
-
|
24
|
-
def initialize(options = {})
|
25
|
-
self.options = options
|
26
|
-
self.config = TerrImporter::Application::Configuration.new options[:config_file]
|
27
|
-
|
28
|
-
end
|
29
|
-
|
30
|
-
def run
|
31
|
-
self.config.load_configuration
|
32
|
-
|
33
|
-
if options[:all] != nil and options[:all] == true
|
34
|
-
import_js
|
35
|
-
import_css
|
36
|
-
import_images
|
37
|
-
else
|
38
|
-
|
39
|
-
options.each do |option, value|
|
40
|
-
if option.to_s =~ /^import_/ and value == true
|
41
|
-
#test this really good!!!
|
42
|
-
self.send option.to_s
|
43
|
-
|
44
|
-
end
|
45
|
-
|
46
|
-
|
47
|
-
end
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
def import_css
|
52
|
-
unclean_suffix = "_unclean"
|
53
|
-
|
54
|
-
check_and_create_dir config['stylesheets']['dest']
|
55
|
-
|
56
|
-
#create stylesheet array and add base.css
|
57
|
-
styles = config['stylesheets']['styles'].split(" ")
|
58
|
-
styles << "base"
|
59
|
-
|
60
|
-
styles.each do |css|
|
61
|
-
destination_path = File.join(config['stylesheets']['dest'], css + ".css")
|
62
|
-
options = {}
|
63
|
-
options[:suffix] = css if css.include?('ie') #add ie option if in array
|
64
|
-
|
65
|
-
source_url = construct_export_request(:css, options)
|
66
|
-
run_download(source_url, destination_path + unclean_suffix)
|
67
|
-
|
68
|
-
#do line replacement
|
69
|
-
File.open(destination_path, 'w') do |d|
|
70
|
-
File.open(destination_path + unclean_suffix, 'r') do |s|
|
71
|
-
lines = s.readlines
|
72
|
-
lines.each do |line|
|
73
|
-
d.print stylesheet_replace_strings(line)
|
74
|
-
end
|
75
|
-
end
|
76
|
-
end
|
77
|
-
FileUtils.remove destination_path + unclean_suffix
|
78
|
-
end
|
79
|
-
end
|
80
|
-
|
81
|
-
def import_js
|
82
|
-
destination_path = File.join(config['javascripts']['dest'], "base.js")
|
83
|
-
js_source_url = construct_export_request :js
|
84
|
-
puts "Importing base.js from #{js_source_url} to #{destination_path}"
|
85
|
-
run_download(js_source_url, destination_path)
|
86
|
-
|
87
|
-
#start library import
|
88
|
-
|
89
|
-
libraries_destination_path = File.join(config['javascripts']['dest'], config['javascripts']['libraries_dest'])
|
90
|
-
|
91
|
-
check_and_create_dir libraries_destination_path
|
92
|
-
|
93
|
-
js_libraries = config['javascripts']['dynamic_libraries'].split(" ")
|
94
|
-
|
95
|
-
puts "Importing libraries from #{config['libraries_source_path']} to #{libraries_destination_path}"
|
96
|
-
|
97
|
-
js_libraries.each do |lib|
|
98
|
-
run_download(config['libraries_source_path'] + lib + ".js", File.join(libraries_destination_path, lib + ".js"))
|
99
|
-
end
|
100
|
-
|
101
|
-
end
|
102
|
-
|
103
|
-
def import_images
|
104
|
-
config['images'].each do |image|
|
105
|
-
check_and_create_dir image['dest']
|
106
|
-
image_source_path = File.join(config['image_base_path'], image['src'])
|
107
|
-
batch_download_files(image_source_path, image['dest'], image['types'])
|
108
|
-
end
|
109
|
-
end
|
110
|
-
|
111
|
-
private
|
112
|
-
|
113
|
-
def batch_download_files(relative_source_path, relative_dest_path, type_filter = "")
|
114
|
-
source_path = relative_source_path
|
115
|
-
|
116
|
-
puts "Downloading files from #{config['url']}#{source_path} to #{relative_dest_path} #{"allowed extensions: " + type_filter unless type_filter.empty?}"
|
117
|
-
|
118
|
-
files = get_file_list(source_path)
|
119
|
-
|
120
|
-
unless type_filter.empty?
|
121
|
-
puts "Appling type filter: #{type_filter}"
|
122
|
-
files = files.find_all { |file| file =~ Regexp.new(".*" + type_filter.strip.gsub(" ", "|") + "$") }
|
123
|
-
end
|
124
|
-
|
125
|
-
puts "Downloading #{files.size} files..."
|
126
|
-
files.each do |file|
|
127
|
-
destination_path = File.join(relative_dest_path, file)
|
128
|
-
run_download(source_path + file, destination_path, :remove_old => false)
|
129
|
-
end
|
130
|
-
end
|
131
|
-
|
132
|
-
def get_file_list(source_path)
|
133
|
-
output = run_download(source_path)
|
134
|
-
files = []
|
135
|
-
|
136
|
-
output.scan(/<a\shref=\"([^\"]+)\"/) { |res| files << res[0] if not res[0] =~ /^\?/ and res[0].size > 1 }
|
137
|
-
files
|
138
|
-
end
|
139
|
-
|
140
|
-
def construct_export_request(for_what = :js, options={})
|
141
|
-
raise "Specify js or css url" unless for_what == :js or for_what == :css
|
142
|
-
export_settings = config['export_settings'].clone
|
143
|
-
|
144
|
-
export_settings['application'] = config['app_path']
|
145
|
-
export_settings.merge!(options)
|
146
|
-
export_settings['appbaseurl'] = "" if for_what == :css
|
147
|
-
|
148
|
-
#glue everything together
|
149
|
-
export_path = config['export_path']
|
150
|
-
export_path.insert(0, "/") unless export_path.match(/^\//)
|
151
|
-
|
152
|
-
export_path = export_path % [for_what.to_s, config['version']] #replace placeholders
|
153
|
-
export_path << '?' << export_settings.map { |k, v| "#{URI.escape(k.to_s)}=#{URI.escape(v.to_s)}" }.join("&")
|
154
|
-
export_path
|
155
|
-
end
|
156
|
-
|
157
|
-
def check_and_create_dir(dir, create = true)
|
158
|
-
unless File.directory?(dir)
|
159
|
-
puts "Directory #{dir} does not exists... it will #{"not" unless true} be created"
|
160
|
-
FileUtils.mkpath(dir) if create
|
161
|
-
end
|
162
|
-
end
|
163
|
-
|
164
|
-
def stylesheet_replace_strings(line)
|
165
|
-
config['stylesheets']['replace'].each do |replace|
|
166
|
-
what = replace['what']
|
167
|
-
with = replace['with']
|
168
|
-
what = Regexp.new "/#{$1}" if what.match(/^r\//)
|
169
|
-
line.gsub! what, with
|
170
|
-
end
|
171
|
-
line
|
172
|
-
end
|
173
|
-
|
174
|
-
#todo use as central download processing
|
175
|
-
def run_download(remote_path, local = nil, options = {})
|
176
|
-
FileUtils.remove_file(local) if not local.nil? and File.exists?(local) and not File.directory?(local) and not options[:remove_old] == true
|
177
|
-
remote_url = config['url'] + remote_path
|
178
|
-
|
179
|
-
case config['downloader']
|
180
|
-
when 'curl'
|
181
|
-
output = `curl '#{remote_url}' #{"> #{local}" if local != nil}`
|
182
|
-
raise "FATAL: An error orrured downloading from #{remote_url} #{"to #{local}: \n #{output}" if local != nil}" if output.include?('error')
|
183
|
-
return output
|
184
|
-
when 'wget'
|
185
|
-
#todo
|
186
|
-
end
|
187
|
-
|
188
|
-
end
|
189
|
-
end
|
190
|
-
end
|
data/public/javascripts/base.js
DELETED
File without changes
|
File without changes
|
File without changes
|
@@ -1,23 +0,0 @@
|
|
1
|
-
require "helper"
|
2
|
-
require "FileUtils"
|
3
|
-
|
4
|
-
|
5
|
-
class TerrImporterTest < Test::Unit::TestCase
|
6
|
-
|
7
|
-
def teardown
|
8
|
-
FileUtils.rm_rf File.join(Dir.pwd, TerrImporter::Application::Configuration::CONFIG_DEFAULT_NAME)
|
9
|
-
end
|
10
|
-
|
11
|
-
should 'merge environment and argument options' do
|
12
|
-
ENV['TERRIMPORTER_OPTS'] = '-j -c'
|
13
|
-
merged_options = TerrImporter::Application.build_options([''] + ['-i', '--verbose'])
|
14
|
-
expected_options = {:import_css => true,
|
15
|
-
:import_js => true,
|
16
|
-
:import_images => true,
|
17
|
-
:verbose => true,
|
18
|
-
:input_file => ''}
|
19
|
-
|
20
|
-
assert_contains merged_options, expected_options
|
21
|
-
end
|
22
|
-
|
23
|
-
end
|