namespacer 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in namespacer.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Brad Carson
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,59 @@
1
+ # Namespacer
2
+
3
+ I built this gem to ease the pain of transitioning a project to Scene7, which requires unique image filenames.
4
+
5
+ Namespacer will search the path for all JPG, PNG and GIF images. Images that are found are renamed with a given prefix. Any reference to these images in JS, HTML, SASS, and CSS file will be updated accordingly.
6
+
7
+ There may be instances where an image is referenced but not found in path (e.g. referencing images outside path in a common folder). You can optionally specific an alternate prefix for these images. For example, I might prefix images found in path with "project-year-subfolder-" and alternate prefix other images "project-year-common-".
8
+
9
+ If you are using Scene7, check out [Scene7ize](https://github.com/codecarson/scene7ize).
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ gem 'namespacer'
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install namespacer
24
+
25
+ ## Usage
26
+
27
+ Main usage:
28
+ `namespacer prefix -p my-prefix-`
29
+
30
+ With an alternate prefix:
31
+ `namespacer prefix -p my-prefix- -a my-alt-prefix-`
32
+
33
+ If you ran this command in this folder:
34
+ - index.html
35
+ - sass/page.sass
36
+ - css/page.css
37
+ - images/dancing-happy-face.gif
38
+ - images/logo.png
39
+
40
+ The images would be renamed:
41
+ - images/my-prefix-dancing-happy-face.gif
42
+ - images/my-prefix-logo.png
43
+
44
+ And any references to them in index.html, page.sass and page.css would be updated.
45
+
46
+ ## TODO
47
+
48
+ - Add options for file filters, include and exclude file types.
49
+ - Add option to run in safe mode that will display what actions would be done without doing them.
50
+ - Refactor the code. I did the bare minimum to make it a gem.
51
+ - Write some tests. I know, I know.
52
+
53
+ ## Contributing
54
+
55
+ 1. Fork it
56
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
57
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
58
+ 4. Push to the branch (`git push origin my-new-feature`)
59
+ 5. Create new Pull Request
data/README.rdoc ADDED
@@ -0,0 +1,19 @@
1
+ = namespacer
2
+
3
+ Description goes here.
4
+
5
+ == Contributing to namespacer
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
9
+ * Fork the project.
10
+ * Start a feature/bugfix branch.
11
+ * Commit and push until you are happy with your contribution.
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2013 Brad Carson. See LICENSE.txt for
18
+ further details.
19
+
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/namespacer ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'namespacer'
4
+ require 'namespacer/cli'
5
+
6
+ Namespacer::CLI.start
@@ -0,0 +1,51 @@
1
+ require 'thor'
2
+
3
+ module Namespacer
4
+
5
+ # Facade for Namespacer command line interface managed by [Thor](https://github.com/wycats/thor).
6
+ # This is the main interface to Namespacer that is called by the Namespacer binary `bin/Namespacer`.
7
+ # Do not put any logic in here, create a class and delegate instead.
8
+ class CLI < Thor
9
+
10
+ require 'namespacer'
11
+ require 'namespacer/version'
12
+
13
+ desc 'prefix', 'Attaches prefix to images found, replacing references in text files'
14
+ method_option :prefix,
15
+ :type => :string,
16
+ :aliases => '-p',
17
+ :banner => 'filename prefix',
18
+ :required => true
19
+
20
+ method_option :alt_prefix,
21
+ :type => :string,
22
+ :aliases => '-a',
23
+ :banner => 'alternate filename prefix for images referenced in text files but not found in path',
24
+ :required => false
25
+
26
+ def prefix
27
+
28
+ Namespacer.prefix(options[:prefix], options[:alt_prefix])
29
+
30
+ end
31
+
32
+
33
+ desc 'version', 'Show the Guard version'
34
+ map %w(-v --version) => :version
35
+
36
+ # Shows the current version of Namespacer.
37
+ #
38
+ # @see Namespacer::VERSION
39
+ #
40
+ def version
41
+ puts "Namespacer version #{ ::Namespacer::VERSION }"
42
+ end
43
+
44
+ private
45
+
46
+ def method_missing(meth, *args)
47
+ help
48
+ end
49
+ end
50
+
51
+ end
@@ -0,0 +1,3 @@
1
+ module Namespacer
2
+ VERSION = "0.1.0"
3
+ end
data/lib/namespacer.rb ADDED
@@ -0,0 +1,118 @@
1
+ require "namespacer/version"
2
+ require 'yaml'
3
+
4
+ module Namespacer
5
+
6
+ def self.prefix(prefix = "", alt_prefix = "")
7
+ ########################################################
8
+ #
9
+ # Namespaces images and replaces references where found.
10
+ # It won't namespace images not found in the path, so if you're referencing
11
+ # images outside of the main folder keep that in mind.
12
+ #
13
+ # Note: takes a long time processing minified files!
14
+ #
15
+ # first argument is the prefix to use
16
+ # second argument is the alternate prefix to use
17
+
18
+
19
+ # GLOB pattern for files to process - cannot pass this pattern as an argument - doesn't work!
20
+ files_to_process = "**/*.{html,sass,css,js}"
21
+
22
+ #####
23
+ #
24
+ # Glob all images in path and subfolders. We'll search for these images
25
+ # inside the HTML file and if found, prefix them.
26
+ #
27
+ original_images_with_path = Dir.glob('**/*.{jpg,png,gif}')
28
+
29
+ # strip path from the match pattern
30
+ original_images = original_images_with_path.map do |img|
31
+ puts File.basename(img)
32
+ File.basename(img)
33
+ end
34
+
35
+ # form a regex match pattern of all of the images, ensuring it's not already namespaced
36
+ images_match_pattern = Regexp.union(original_images.map { |img| /(?<!#{prefix})#{img}/ })
37
+
38
+
39
+
40
+ #####
41
+ #
42
+ # Create the replacement hash but prefixing filename
43
+ #
44
+ replacement_hash = original_images.inject({}) do |m,e|
45
+ m[e] = e.gsub(e, File.basename(prefix + e))
46
+ m
47
+ end
48
+
49
+ namespaced = original_images.map { |old_name| prefix + old_name }
50
+
51
+
52
+ ######################################################################
53
+ #
54
+ # Loop through each file and replace the original image name with the
55
+ # namespaced one.
56
+ #
57
+ puts "------------------------------------------------------------------"
58
+
59
+ Dir.glob(files_to_process).each do |input_file|
60
+ puts "\n\rWorking on: #{input_file}"
61
+
62
+ file_content = File.read(input_file)
63
+ replacement = file_content.gsub(images_match_pattern) do |match|
64
+ new_string = match
65
+
66
+ if Regexp.new("^#{prefix}") =~ match
67
+ puts "Ignoring: #{match} - already namespaced"
68
+ else
69
+ puts "-- replacing #{match} --> #{replacement_hash[match]}"
70
+ new_string = replacement_hash[match]
71
+ end
72
+
73
+ new_string
74
+ end
75
+
76
+ # TODO: add an option to accept an argument to namespace files not found inside
77
+ # path (e.g. referenced outside main folder)
78
+
79
+ puts 'Other images referenced:'
80
+ replacement = replacement.gsub(/(?<!\\\/)([^\\\/]+)(gif|jpg|png)/) do |match|
81
+ new_string = match
82
+
83
+ # ignore match if it has one of our prefixes
84
+ prefixes = [prefix, alt_prefix].reject {|x| x.nil? || x.empty?} .join('|')
85
+ unless match =~ Regexp.new("^#{prefixes}")
86
+ if alt_prefix && !alt_prefix.empty?
87
+ puts "-- #{match} --> #{alt_prefix}#{match}"
88
+ new_string = "#{alt_prefix}#{match}"
89
+ end
90
+ puts "-- #{match}"
91
+ end
92
+
93
+ new_string
94
+ end
95
+
96
+
97
+ # write to file
98
+ File.open(input_file, "w") { |file| file.puts replacement }
99
+ end
100
+
101
+
102
+ ######################################################################
103
+ #
104
+ # Rename files
105
+ #
106
+ original_images_with_path.each do |old_file|
107
+ basename = File.basename(old_file)
108
+ new_file = old_file.gsub(basename, replacement_hash[basename])
109
+
110
+ unless old_file.include?(prefix)
111
+ puts "Renaming #{old_file} -> #{new_file}"
112
+ File.rename(old_file, new_file)
113
+ end
114
+ end
115
+
116
+ end
117
+
118
+ end
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'namespacer/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "namespacer"
8
+ gem.version = Namespacer::VERSION
9
+ gem.authors = ["Brad Carson"]
10
+ gem.email = ["brad@pixelwavedesign.com"]
11
+ gem.description = %q{...}
12
+ gem.summary = %q{Prefixes images with a given string, renaming files and replacing references in text files}
13
+ gem.homepage = "https://github.com/codecarson/namespacer"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_development_dependency 'rake'
21
+ gem.add_development_dependency 'rspec'
22
+
23
+ gem.add_dependency 'thor'
24
+ gem.add_dependency 'mini_magick'
25
+
26
+ end
data/namespacer.rb ADDED
@@ -0,0 +1,117 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'yaml'
4
+
5
+ ########################################################
6
+ #
7
+ # Namespaces images and replaces references where found.
8
+ # It won't namespace images not found in the path, so if you're referencing
9
+ # images outside of the main folder keep that in mind.
10
+ #
11
+ # Note: takes a long time processing minified files!
12
+ #
13
+ # first argument is the prefix to use
14
+ # second argument is the alternate prefix to use
15
+
16
+ # parse arguments
17
+
18
+ prefix = ""
19
+ suffix = ""
20
+
21
+ # GLOB pattern for files to process - cannot pass this pattern as an argument - doesn't work!
22
+ files_to_process = "**/*.{html,sass,css,js}"
23
+
24
+ # grab the prefix
25
+ prefix = ARGV.first || ""
26
+ alt_prefix = ARGV[1] || "" # alternate prefixing for images not found in path
27
+
28
+
29
+ #####
30
+ #
31
+ # Glob all images in path and subfolders. We'll search for these images
32
+ # inside the HTML file and if found, prefix them.
33
+ #
34
+ original_images_with_path = Dir.glob('**/*.{jpg,png,gif}')
35
+
36
+ # strip path from the match pattern
37
+ original_images = original_images_with_path.map do |img|
38
+ puts File.basename(img)
39
+ File.basename(img)
40
+ end
41
+
42
+ # form a regex match pattern of all of the images, ensuring it's not already namespaced
43
+ images_match_pattern = Regexp.union(original_images.map { |img| /(?<!#{prefix})#{img}/ })
44
+
45
+
46
+
47
+ #####
48
+ #
49
+ # Create the replacement hash but prefixing filename
50
+ #
51
+ replacement_hash = original_images.inject({}) do |m,e|
52
+ m[e] = e.gsub(e, File.basename(prefix + e))
53
+ m
54
+ end
55
+
56
+ puts "Replacement Hash: "
57
+ puts replacement_hash.to_yaml
58
+
59
+ namespaced = original_images.map { |old_name| prefix + old_name }
60
+ puts "\n\rOriginal images: #{Regexp.union(namespaced)}"
61
+
62
+
63
+
64
+ ######################################################################
65
+ #
66
+ # Loop through each file and replace the original image name with the
67
+ # namespaced one.
68
+ #
69
+ puts "\n\rImage match pattern: #{images_match_pattern}"
70
+ puts "------------------------------------------------------------------"
71
+
72
+ Dir.glob(files_to_process).each do |input_file|
73
+ puts "\n\rWorking on: #{input_file}"
74
+
75
+ file_content = File.read(input_file)
76
+ replacement = file_content.gsub(images_match_pattern) do |match|
77
+ puts "-- replacing #{match} --> #{replacement_hash[match]}"
78
+ replacement_hash[match]
79
+ end
80
+
81
+ # TODO: add an option to accept an argument to namespace files not found inside
82
+ # path (e.g. referenced outside main folder)
83
+
84
+ puts 'Other images referenced:'
85
+ replacement = replacement.gsub(/(?<!\\\/)([^\\\/]+)(gif|jpg|png)/) do |match|
86
+ new_string = match
87
+
88
+ # ignore match if it has one of our prefixes
89
+ prefixes = [prefix, alt_prefix].reject {|x| x.empty?} .join('|')
90
+ unless match =~ Regexp.new("^#{prefixes}")
91
+ unless alt_prefix.empty?
92
+ puts "-- #{match} --> #{alt_prefix}-#{match}"
93
+ new_string = "#{alt_prefix}-#{match}"
94
+ end
95
+ puts "-- #{match}"
96
+ end
97
+
98
+ new_string
99
+ end
100
+
101
+
102
+ # write to file
103
+ File.open(input_file, "w") { |file| file.puts replacement }
104
+ end
105
+
106
+
107
+ ######################################################################
108
+ #
109
+ # Rename files
110
+ #
111
+ original_images_with_path.each do |old_file|
112
+ basename = File.basename(old_file)
113
+ new_file = old_file.gsub(basename, replacement_hash[basename])
114
+
115
+ puts "Renaming #{old_file} -> #{new_file}"
116
+ File.rename(old_file, new_file) unless old_file.include?(prefix)
117
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,18 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'test/unit'
11
+ require 'shoulda'
12
+
13
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
14
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
15
+ require 'namespacer'
16
+
17
+ class Test::Unit::TestCase
18
+ end
@@ -0,0 +1,7 @@
1
+ require 'helper'
2
+
3
+ class TestNamespacer < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,128 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: namespacer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Brad Carson
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-03-19 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: thor
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: mini_magick
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ description: ! '...'
79
+ email:
80
+ - brad@pixelwavedesign.com
81
+ executables:
82
+ - namespacer
83
+ extensions: []
84
+ extra_rdoc_files: []
85
+ files:
86
+ - .document
87
+ - .gitignore
88
+ - Gemfile
89
+ - LICENSE.txt
90
+ - README.md
91
+ - README.rdoc
92
+ - Rakefile
93
+ - bin/namespacer
94
+ - lib/namespacer.rb
95
+ - lib/namespacer/cli.rb
96
+ - lib/namespacer/version.rb
97
+ - namespacer.gemspec
98
+ - namespacer.rb
99
+ - test/helper.rb
100
+ - test/test_namespacer.rb
101
+ homepage: https://github.com/codecarson/namespacer
102
+ licenses: []
103
+ post_install_message:
104
+ rdoc_options: []
105
+ require_paths:
106
+ - lib
107
+ required_ruby_version: !ruby/object:Gem::Requirement
108
+ none: false
109
+ requirements:
110
+ - - ! '>='
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ required_rubygems_version: !ruby/object:Gem::Requirement
114
+ none: false
115
+ requirements:
116
+ - - ! '>='
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ requirements: []
120
+ rubyforge_project:
121
+ rubygems_version: 1.8.24
122
+ signing_key:
123
+ specification_version: 3
124
+ summary: Prefixes images with a given string, renaming files and replacing references
125
+ in text files
126
+ test_files:
127
+ - test/helper.rb
128
+ - test/test_namespacer.rb