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.
Files changed (51) hide show
  1. data/.gitignore +1 -0
  2. data/Gemfile.lock +6 -9
  3. data/Rakefile +69 -0
  4. data/bin/terrimporter +5 -1
  5. data/config/schema.yml +0 -3
  6. data/config/terrimporter.config.yml +4 -7
  7. data/lib/{app_logger.rb → terrimporter/app_logger.rb} +0 -0
  8. data/lib/terrimporter/config_helper.rb +46 -0
  9. data/lib/{config_validator.rb → terrimporter/config_validator.rb} +0 -0
  10. data/lib/terrimporter/configuration.rb +72 -0
  11. data/lib/terrimporter/downloader.rb +38 -0
  12. data/lib/terrimporter/importer.rb +185 -0
  13. data/lib/{options.rb → terrimporter/options.rb} +16 -8
  14. data/lib/terrimporter/version.rb +3 -2
  15. data/lib/terrimporter.rb +30 -26
  16. data/tasks/load_tasks.rb +3 -0
  17. data/tasks/version.rake +2 -0
  18. data/terrimporter.gemspec +7 -4
  19. data/test/fixtures/css/base.css +1 -0
  20. data/test/fixtures/css/ie.css +1 -0
  21. data/test/fixtures/html/img_backgrounds_dir.html +43 -0
  22. data/test/fixtures/html/img_dir.html +67 -0
  23. data/test/fixtures/html/js_dyn_dir.html +44 -0
  24. data/test/fixtures/img/background.jpg +0 -0
  25. data/test/fixtures/img/testimage.png +0 -0
  26. data/test/fixtures/invalid.config.yml +27 -0
  27. data/test/fixtures/js/base.js +1 -0
  28. data/test/fixtures/js/dynlib.js +1 -0
  29. data/test/fixtures/test.config.yml +64 -0
  30. data/test/helper.rb +0 -19
  31. data/test/terrimporter.config.yml +68 -0
  32. data/test/test/tmp/public/javascripts/base.js +1 -0
  33. data/test/test/tmp/public/javascripts/lib/dynlib.js +1 -0
  34. data/test/test/tmp/public/stylesheets/base.css +1 -0
  35. data/test/test/tmp/public/stylesheets/ie.css +1 -0
  36. data/test/test_helper.rb +47 -0
  37. data/test/test_terrimporter.rb +66 -4
  38. data/test/unit/test_app_logger.rb +51 -0
  39. data/test/unit/test_config_helper.rb +33 -0
  40. data/test/unit/test_config_validator.rb +20 -0
  41. data/test/unit/test_configuration.rb +30 -26
  42. data/test/unit/test_downloader.rb +38 -0
  43. data/test/unit/test_importer.rb +159 -23
  44. data/test/unit/test_options.rb +45 -7
  45. metadata +80 -24
  46. data/lib/configuration.rb +0 -93
  47. data/lib/importer.rb +0 -190
  48. data/public/javascripts/base.js +0 -0
  49. data/public/javascripts/pngfix.js +0 -0
  50. data/public/javascripts/warning.js +0 -0
  51. data/test/unit/test_terrimporter.rb +0 -23
data/.gitignore CHANGED
@@ -6,3 +6,4 @@ doc
6
6
  .bundle
7
7
  Gemfile.lock
8
8
  pkg/*
9
+ test/tmp
data/Gemfile.lock CHANGED
@@ -1,20 +1,16 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- terrimporter (0.4.0)
5
- kwalify
4
+ terrimporter (0.5.3)
5
+ kwalify (>= 0.7.2)
6
6
 
7
7
  GEM
8
8
  remote: http://rubygems.org/
9
9
  specs:
10
- git (1.2.5)
11
- jeweler (1.6.4)
12
- bundler (~> 1.0)
13
- git (>= 1.2.5)
14
- rake
10
+ fakeweb (1.3.0)
15
11
  kwalify (0.7.2)
16
12
  rake (0.9.2)
17
- rcov (0.9.9)
13
+ rcov (0.9.10)
18
14
  shoulda (2.11.3)
19
15
 
20
16
  PLATFORMS
@@ -22,7 +18,8 @@ PLATFORMS
22
18
 
23
19
  DEPENDENCIES
24
20
  bundler (~> 1.0.0)
25
- jeweler (~> 1.6.4)
21
+ fakeweb
22
+ rake
26
23
  rcov
27
24
  shoulda
28
25
  terrimporter!
data/Rakefile CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'bundler'
2
+ require 'rake'
2
3
  Bundler::GemHelper.install_tasks
3
4
 
4
5
  require 'rake/testtask'
@@ -18,6 +19,7 @@ end
18
19
 
19
20
  task :default => :test
20
21
 
22
+ #todo this is wrong!
21
23
  require 'rake/rdoctask'
22
24
  Rake::RDocTask.new do |rdoc|
23
25
  version = File.exist?('VERSION') ? File.read('VERSION') : ""
@@ -27,3 +29,70 @@ Rake::RDocTask.new do |rdoc|
27
29
  rdoc.rdoc_files.include('README*')
28
30
  rdoc.rdoc_files.include('lib/**/*.rb')
29
31
  end
32
+
33
+
34
+ namespace :version do
35
+
36
+ namespace :bump do
37
+ desc "Bump major version"
38
+ task :major do
39
+ bump_version :major
40
+ end
41
+
42
+ desc "Bump minor version"
43
+ task :minor do
44
+ bump_version :minor
45
+ end
46
+
47
+ desc "Bump patch version"
48
+ task :patch do
49
+ bump_version :patch
50
+ end
51
+ end
52
+
53
+ def bump_version(version_to_bump)
54
+ puts "Pumping version"
55
+ version = version?
56
+ case version_to_bump
57
+ when :major
58
+ version[:major] = version[:major].to_i + 1
59
+ when :minor
60
+ version[:minor] = version[:minor].to_1 + 1
61
+ when :patch
62
+ version[:patch] = version[:patch].to_i + 1
63
+ end
64
+ puts "New version; " + version_string(version)
65
+ write_version(version)
66
+ end
67
+
68
+ def version_file_path
69
+ File.join(File.dirname(__FILE__), 'lib', 'terrimporter', 'version.rb')
70
+ end
71
+
72
+ def version?
73
+ pattern = /(\d+).(\d+).(\d+)/
74
+ version = nil
75
+ version_file = File.read(version_file_path)
76
+ version_file.scan(pattern) do |match|
77
+ version = {:major => match[0], :minor => match[1], :patch => match[2]}
78
+ end
79
+ version
80
+ end
81
+
82
+ def version_string(version)
83
+ "#{version[:major]}.#{version[:minor]}.#{version[:patch]}"
84
+ end
85
+
86
+ def write_version(version={})
87
+ version_rb = %Q{#Generated by rake task, last bump: #{version.to_s}
88
+ module TerrImporter
89
+ VERSION = "#{version_string version}"
90
+ end
91
+ }
92
+ File.open(version_file_path, 'w') { |f| f.write(version_rb) }
93
+ end
94
+
95
+
96
+ end
97
+
98
+
data/bin/terrimporter CHANGED
@@ -1,6 +1,10 @@
1
1
  #!/usr/bin/env ruby
2
+ ###
3
+ ### $Rev$
4
+ ### $Release: 0.4.0 $
5
+ ###
6
+
2
7
  $LOAD_PATH.unshift File.join(File.dirname(__FILE__), '..', 'lib')
3
- require 'rubygems'
4
8
  require 'terrimporter'
5
9
 
6
10
  exit TerrImporter::Application.run!(*ARGV)
data/config/schema.yml CHANGED
@@ -23,9 +23,6 @@ mapping:
23
23
  type: str
24
24
  required: yes
25
25
  pattern: /\/.*/
26
- "downloader":
27
- type: str
28
- enum: [curl, wget]
29
26
  "export_settings":
30
27
  type: map
31
28
  mapping:
@@ -7,7 +7,6 @@
7
7
  # export_path You most probably won't have to change this
8
8
  # image_base_path: The Terrific image base path. You most probably won't have to change this
9
9
  # libraries_source_path: The library source path. You most probably won't have to change this
10
- # downloader: The downloader to use, supported are 'curl' and 'wget'
11
10
  #
12
11
  # export_settings: The settings below are added as query parameters for base.js and base.css
13
12
  # layout: You most probably won't have to change this
@@ -19,7 +18,7 @@
19
18
  # styles: Name of the stylesheets, space separated without the .css extension. ex: ie ie6 ie7 ie8
20
19
  # the main style base.css is included by default and MUST NOT BE SPECIFIED
21
20
  # replace: Define path replacements after the stylesheets have been downloaded
22
- # you can define multiple replacements by repeating the '-what: with:' pattern:
21
+ # you can define multiple replacements by repeating the '-what: with:' statements
23
22
  # - what: What to replace, default is string. if you want to use regex, use the format r/[regex]/
24
23
  # for help on regular expressions see http://www.rubular.com/ (an online editor/tester)
25
24
  # with: Replace with this (string)
@@ -37,12 +36,10 @@
37
36
  ########################################################################################################################
38
37
  version: 0.5
39
38
  url: http://terrific.url
40
- app_path: /Users/danielkummer/_NAMICS/projekte/mobilezone/mobilezone-abo_optimizer/public/terrific
39
+ app_path: /terrific/webapp/path
41
40
  export_path: /terrific/base/%2$s/public/%1$s/base/base.%1$s.php
42
- libraries_source_path: /js/libraries/dynamic/
43
- #todo remove backslash
41
+ libraries_source_path: /js/libraries/dynamic
44
42
  image_base_path: /img
45
- downloader: curl
46
43
  export_settings:
47
44
  layout: project
48
45
  debug: false
@@ -55,7 +52,7 @@
55
52
  with: /images/
56
53
  javascripts:
57
54
  dest: public/javascripts/
58
- dynamic_libraries: warning pngfix
55
+ dynamic_libraries: dynlib
59
56
  libraries_dest: /
60
57
  images:
61
58
  - src: /
File without changes
@@ -0,0 +1,46 @@
1
+
2
+ module ConfigHelper
3
+
4
+ def config_default_name
5
+ 'terrimporter.config.yml'
6
+ end
7
+
8
+ def schema_default_name
9
+ 'schema.yml'
10
+ end
11
+
12
+ def config_working_directory_path
13
+ File.expand_path config_default_name
14
+ end
15
+
16
+ def config_working_directory_exists?
17
+ File.exists? config_working_directory_path
18
+ end
19
+
20
+ def config_example_path
21
+ File.join(base_config_path, config_default_name)
22
+ end
23
+
24
+ def schema_file_path
25
+ File.join(base_config_path, schema_default_name)
26
+ end
27
+
28
+ def create_config_file(backup_or_replace = nil)
29
+ puts "Creating configuration file"
30
+ case backup_or_replace
31
+ when :backup
32
+ puts "Backing up old configuration file to #{config_working_directory_path}.bak"
33
+ FileUtils.mv(config_working_directory_path, config_working_directory_path + '.bak')
34
+ when :replace
35
+ puts "Replacing old configuration file"
36
+ FileUtils.rm_f(config_working_directory_path) if File.exists? config_working_directory_path
37
+ end
38
+ FileUtils.cp(config_example_path, config_working_directory_path)
39
+ end
40
+
41
+ private
42
+ def base_config_path
43
+ File.join(File.dirname(__FILE__), '..', '..', 'config')
44
+ end
45
+
46
+ end
@@ -0,0 +1,72 @@
1
+ require 'etc'
2
+ require 'kwalify'
3
+ require 'config_validator'
4
+ require 'config_helper'
5
+ require 'configuration'
6
+
7
+ module TerrImporter
8
+ class Application
9
+ class Configuration < Hash
10
+ include ConfigHelper
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
+ puts "Configuration file located, load from #{config_file_path}"
22
+ validate_and_load_config(config_file_path)
23
+ end
24
+
25
+ def determine_config_file_path
26
+ unless self.config_file.nil?
27
+ return self.config_file
28
+ end
29
+
30
+ valid_config_paths.each do |path|
31
+ file_path = File.join path, config_default_name
32
+ return file_path if File.exists?(file_path) and not file_path.include?(File.join('terrimporter', 'config')) #default config NOT valid
33
+ end
34
+
35
+ raise ConfigurationError, %Q{config file #{config_default_name} not found in search paths. Search paths are:
36
+ #{valid_config_paths.join "\n"} \n If this is a new project, run with the option --init}
37
+ end
38
+
39
+ def valid_config_paths
40
+ [
41
+ Dir.pwd,
42
+ File.join(Dir.pwd, 'config'),
43
+ File.join(Dir.pwd, '.config'),
44
+ Etc.getpwuid.dir
45
+ ]
46
+ end
47
+
48
+ #todo split!
49
+ def validate_and_load_config(file)
50
+ puts "Validating configuration..."
51
+
52
+ parser = Kwalify::Yaml::Parser.new(load_validator)
53
+ document = parser.parse_file(file)
54
+
55
+ errors = parser.errors()
56
+ if errors && !errors.empty?
57
+ error_message = errors.inject("") { |result, e| result << "#{e.linenum}:#{e.column} [#{e.path}] #{e.message}\n" }
58
+ raise ConfigurationError, error_message
59
+ end
60
+ self.merge! document
61
+
62
+ end
63
+
64
+ def load_validator
65
+ puts "Loading validator from #{schema_file_path}"
66
+ schema = Kwalify::Yaml.load_file(schema_file_path)
67
+ ConfigValidator.new(schema)
68
+ end
69
+
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,38 @@
1
+ require 'open-uri'
2
+ require 'uri'
3
+
4
+ module TerrImporter
5
+ class Application
6
+ class Downloader
7
+
8
+ def initialize(base_uri)
9
+ @base_uri = base_uri
10
+ puts "Downloader initialized to uri: #{base_uri}"
11
+ end
12
+
13
+ def download(remote_path, local_path=nil)
14
+ absolute_uri = absolute_path(remote_path)
15
+ if local_path.nil? #download to buffer
16
+ data = StringIO.new
17
+
18
+ puts "Downloading #{absolute_uri} to buffer"
19
+
20
+ absolute_uri.open { |io| data = io.read }
21
+ data.to_s
22
+ else
23
+ puts "Downloading #{absolute_uri} to local path #{local_path}"
24
+
25
+ open(local_path, "wb") { |file|
26
+ file.write(absolute_uri.open.read)
27
+ }
28
+ end
29
+ end
30
+
31
+ private
32
+ def absolute_path(relative_path)
33
+ URI.join(@base_uri, relative_path)
34
+ end
35
+
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,185 @@
1
+ require 'fileutils'
2
+ require 'yaml'
3
+ require 'uri'
4
+
5
+ module TerrImporter
6
+
7
+ class DefaultError < StandardError
8
+ end
9
+
10
+ class ConfigurationError < StandardError
11
+ end
12
+
13
+ class ConfigurationMissingError < StandardError
14
+ end
15
+
16
+ class Application
17
+ class Importer
18
+ include Logging
19
+
20
+ attr_accessor :options, :config
21
+
22
+ def initialize(options = {})
23
+ self.options = options
24
+ self.config = Configuration.new options[:config_file]
25
+ self.config.load_configuration
26
+ @downloader = Downloader.new config['url']
27
+ end
28
+
29
+ def run
30
+ if options[:all] != nil and options[:all] == true
31
+ puts "Import of everything started"
32
+ import_js
33
+ import_css
34
+ import_images
35
+ else
36
+ options.each do |option, value|
37
+ if option.to_s =~ /^import_/ and value == true
38
+ puts "Import of #{option.to_s.split('_').last} started"
39
+ self.send option.to_s
40
+ end
41
+ end
42
+ end
43
+ end
44
+
45
+ def import_css
46
+ unclean_suffix = "_unclean"
47
+
48
+ check_and_create_dir config['stylesheets']['dest']
49
+
50
+ #create stylesheet array and add base.css
51
+ styles = config['stylesheets']['styles'].split(" ")
52
+ styles << "base"
53
+
54
+ styles.each do |css|
55
+ destination_path = File.join(config['stylesheets']['dest'], css + ".css")
56
+ options = {}
57
+ options[:suffix] = css if css.include?('ie') #add ie option if in array
58
+
59
+ source_url = construct_export_path(:css, options)
60
+
61
+ @downloader.download(source_url, destination_path + unclean_suffix)
62
+
63
+ #do line replacement
64
+ puts "Start css line replacements"
65
+ File.open(destination_path, 'w') do |d|
66
+ File.open(destination_path + unclean_suffix, 'r') do |s|
67
+ lines = s.readlines
68
+ lines.each do |line|
69
+ d.print stylesheet_replace_strings!(line)
70
+ end
71
+ end
72
+ end
73
+ puts "Deleting unclean css files"
74
+ FileUtils.remove destination_path + unclean_suffix
75
+ end
76
+ end
77
+
78
+ def import_js
79
+ check_and_create_dir config['javascripts']['dest']
80
+ destination_path = File.join(config['javascripts']['dest'], "base.js")
81
+ js_source_url = construct_export_path :js
82
+
83
+ puts "Importing base.js from #{js_source_url} to #{destination_path}"
84
+
85
+ @downloader.download(js_source_url, destination_path)
86
+
87
+
88
+ libraries_destination_path = File.join(config['javascripts']['dest'], config['javascripts']['libraries_dest'])
89
+ check_and_create_dir libraries_destination_path
90
+ js_libraries = config['javascripts']['dynamic_libraries'].split(" ")
91
+
92
+ puts "Importing libraries from #{config['libraries_source_path']} to #{libraries_destination_path}"
93
+
94
+ js_libraries.each do |lib|
95
+ @downloader.download(File.join(config['libraries_source_path'], lib+ ".js"), File.join(libraries_destination_path, lib + ".js"))
96
+ end
97
+ end
98
+
99
+ def import_images
100
+ config['images'].each do |image|
101
+ check_and_create_dir image['dest']
102
+ image_source_path = File.join(config['image_base_path'], image['src'])
103
+ batch_download(image_source_path, image['dest'], image['types'])
104
+ end
105
+ end
106
+
107
+ private
108
+
109
+ def batch_download(relative_source_path, relative_dest_path, type_filter = "")
110
+ source_path = relative_source_path
111
+
112
+ puts "Downloading multiple files from #{config['url']}#{source_path} to #{relative_dest_path} #{"allowed extensions: " + type_filter unless type_filter.empty?}"
113
+
114
+ files = html_directory_content_list(source_path)
115
+
116
+ unless type_filter.empty?
117
+ puts "Appling type filter: #{type_filter}"
118
+ files = files.find_all { |file| file =~ Regexp.new(".*" + type_filter.strip.gsub(" ", "|") + "$") }
119
+ end
120
+
121
+ puts "Downloading #{files.size} files..."
122
+ files.each do |file|
123
+ destination_path = File.join(relative_dest_path, file)
124
+ @downloader.download(File.join(source_path,file), destination_path)
125
+ end
126
+ end
127
+
128
+ def html_directory_content_list(source_path)
129
+ puts "Getting html directory list"
130
+ output = @downloader.download(source_path)
131
+ files = []
132
+
133
+ output.scan(/<a\shref=\"([^\"]+)\"/) do |res|
134
+ files << res[0] if not res[0] =~ /^\?/ and not res[0] =~ /.*\/$/ and res[0].size > 1
135
+ end
136
+ puts "Found #{files.size} files"
137
+ files
138
+ end
139
+
140
+ def construct_export_path(for_what = :js, options={})
141
+ raise DefaultError, "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
+ created_or_exists = false
159
+ unless File.directory?(dir)
160
+ puts "Directory #{dir} does not exists... it will #{"not" unless create} be created"
161
+ if create
162
+ FileUtils.mkpath(dir)
163
+ created_or_exists = true
164
+ end
165
+ else
166
+ created_or_exists = true
167
+ end
168
+ created_or_exists
169
+ end
170
+
171
+ def stylesheet_replace_strings!(line)
172
+ config['stylesheets']['replace'].each do |replace|
173
+ what = replace['what']
174
+ with = replace['with']
175
+ what = Regexp.new "#{$1}" if what.match(/^r\/(.*)\//)
176
+
177
+ puts "Replacing #{what.to_s} with #{with}"
178
+
179
+ line.gsub! what, with
180
+ end
181
+ line
182
+ end
183
+ end
184
+ end
185
+ end
@@ -9,6 +9,7 @@ module TerrImporter
9
9
  @orig_args = args.clone
10
10
 
11
11
  self[:verbose] = true
12
+ self[:show_help] = false
12
13
 
13
14
  require 'optparse'
14
15
  @opts = OptionParser.new do |o|
@@ -24,10 +25,15 @@ module TerrImporter
24
25
  end
25
26
 
26
27
  o.on('-c', '--css', 'export configured css files') { self[:import_css] = true }
28
+
27
29
  o.on('-i', '--img', 'export configured image files') { self[:import_images] = true }
30
+
28
31
  o.on('-j', '--js', 'export configured javascript files') { self[:import_js] = true }
29
- o.on('--init', 'create configuration file in current working directory') { self[:init] = true }
30
- #todo add force option to init
32
+
33
+ o.on('--init [CONFIG_EXISTS]', [:backup, :replace], 'create configuration file in current working directory. use optional argument to force file replacement (backup, replace)') do |init|
34
+ self[:init] = init || true
35
+ end
36
+
31
37
  o.on('-f', '--config CONFIG_FILE', 'use alternative configuration file') do |config_file|
32
38
  self[:config_file] = config_file
33
39
  end
@@ -35,14 +41,12 @@ module TerrImporter
35
41
  o.separator ''
36
42
  o.separator 'Additional configuration:'
37
43
 
38
- #o.on('-v', '--verbose [on, off], default [on]', [true, false], 'Verbose mode') do |v|
39
44
  o.on('-v', '--[no-]verbose', 'run verbosely') do |v|
40
45
  self[:verbose] = v
41
46
  end
42
47
 
43
48
  o.on('--version', 'Show version') do
44
- puts ::File.open(::File.join(File.dirname(__FILE__), "..", "VERSION"), 'r').gets
45
- exit
49
+ self[:show_version] = true
46
50
  end
47
51
 
48
52
  o.on_tail('-h', '--help', 'display this help and exit') { self[:show_help] = true }
@@ -51,7 +55,7 @@ module TerrImporter
51
55
  begin
52
56
  @opts.parse!(args)
53
57
  show_help_on_no_options
54
- self[:input_file] = args.shift
58
+ #self[:input_file] = args.shift #todo remove if really not necessary
55
59
  rescue OptionParser::InvalidOption => e
56
60
  self[:invalid_argument] = e.message
57
61
  end
@@ -62,9 +66,13 @@ module TerrImporter
62
66
  end
63
67
 
64
68
  def show_help_on_no_options
65
- self[:show_help] = true unless self[:import_css] or self[:import_js] or self[:import_images] or self[:init] or self[:version]
69
+ unless self[:import_css] or self[:import_js] or self[:import_images] or self[:init] or self[:version]
70
+ puts "None of the default options selected, showing help"
71
+ self[:show_help] = true
72
+ else
73
+ self[:show_help] = false
74
+ end
66
75
  end
67
-
68
76
  end
69
77
  end
70
78
  end
@@ -1,3 +1,4 @@
1
- module Terrimporter
2
- VERSION = "0.4.0"
1
+ #Generated by rake task, last bump: patch3major1minor5
2
+ module TerrImporter
3
+ VERSION = "0.5.4"
3
4
  end