fork_rails_project 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/fork_rails_project.rb +4 -107
- data/lib/forker.rb +133 -0
- metadata +32 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 183319270c8a7869b9b146514adbefe97baba73a
|
4
|
+
data.tar.gz: 6a78cf625f8030189c909f9a7275ad3b79459598
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6894a38ab8fb7e6de22ece86de809e62e231f99504b3865d7b4f9d13f5ac5e96677cba539125f402639a794f5d4639387cb46a13b8b4e754c6a03afe6677c291
|
7
|
+
data.tar.gz: c72563313cc5fe379a2aaccf951956607d022190239dd8b5febf2382416386087a4e3c794bee68903187068232d37906effe9f287e5cbafe4a9c36845ecd1cac
|
data/lib/fork_rails_project.rb
CHANGED
@@ -1,114 +1,11 @@
|
|
1
1
|
#!/usr/bin/ruby
|
2
|
-
require "
|
3
|
-
require "find"
|
4
|
-
# require "pry"
|
5
|
-
|
6
|
-
|
7
|
-
def copy_files(dest_path, ignored_files)
|
8
|
-
copy_string = "rsync -ax "
|
9
|
-
ignored_files.each do |file_name|
|
10
|
-
copy_string << "--exclude #{file_name} "
|
11
|
-
end
|
12
|
-
copy_string << ". #{dest_path}"
|
13
|
-
begin
|
14
|
-
puts "Copying files from #{Dir.pwd} to #{dest_path}"
|
15
|
-
puts "Ignored files/directories: #{ignored_files.join(' ')}"
|
16
|
-
puts "Executing #{copy_string}"
|
17
|
-
%x[#{copy_string}]
|
18
|
-
puts
|
19
|
-
rescue IOError
|
20
|
-
puts "Unable to copy files!"
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
def rename_file_objects(old_name, new_name)
|
25
|
-
old_file_paths = []
|
26
|
-
Find.find(".") do |path|
|
27
|
-
old_file_paths << path if path =~ /#{old_name}/
|
28
|
-
end
|
29
|
-
old_file_paths = yield(old_file_paths) if block_given?
|
30
|
-
files_to_move = Hash.new
|
31
|
-
old_file_paths.each do |path|
|
32
|
-
new_path = path.gsub(/#{old_name}/, new_name)
|
33
|
-
files_to_move[path] = new_path
|
34
|
-
end
|
35
|
-
files_to_move.each do |old, new|
|
36
|
-
puts "Renaming #{old} -> #{new}"
|
37
|
-
`mv #{old} #{new}`
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
def substitute_names(old_name, new_name)
|
42
|
-
puts "Replacing string '#{old_name}' in application files..."
|
43
|
-
occurrence = %x{grep -iR "#{old_name}" --exclude-dir=log --exclude-dir=tmp --exclude=tags .}
|
44
|
-
occurrence = occurrence.split("\n")
|
45
|
-
files = []
|
46
|
-
occurrence.each do |occ|
|
47
|
-
occ.slice!(0..1)
|
48
|
-
file_name = occ.slice(0...occ.index(":"))
|
49
|
-
files << file_name
|
50
|
-
end
|
51
|
-
files.uniq!
|
52
|
-
|
53
|
-
begin
|
54
|
-
files.each do |file|
|
55
|
-
text = File.read(file)
|
56
|
-
text = text.gsub(old_name, new_name)
|
57
|
-
File.open(file, "w+") { |line| line.puts text }
|
58
|
-
end
|
59
|
-
rescue IOError
|
60
|
-
puts "Unable to alter new files!"
|
61
|
-
end
|
62
|
-
|
63
|
-
puts "Finished replacing string '#{old_name}' in application files. Altered files:"
|
64
|
-
files.each do |filename|
|
65
|
-
puts filename
|
66
|
-
end
|
67
|
-
end
|
2
|
+
require "forker"
|
68
3
|
|
69
4
|
|
70
5
|
source_project_name = ARGV[0].dup
|
71
6
|
dest_project_name = ARGV[1].dup
|
72
|
-
|
73
|
-
folder_names << source_project_name << dest_project_name
|
74
|
-
|
75
|
-
folder_names.each do |a|
|
76
|
-
a.strip!
|
77
|
-
if a.start_with? "/"
|
78
|
-
a.slice!(0)
|
79
|
-
end
|
80
|
-
end
|
81
|
-
|
82
|
-
raise RuntimeError, "#{source_project_name} is not a valid dir!" if !File.directory?("./#{source_project_name}")
|
83
|
-
raise RuntimeError, "Destination directory already exists!" if File.directory?("./#{dest_project_name}")
|
84
|
-
|
85
|
-
base_dir_path = Dir.pwd
|
86
|
-
dest_path = base_dir_path + "/" + dest_project_name
|
87
|
-
ignored_files = ARGV[2..-1]
|
88
|
-
|
89
|
-
begin
|
90
|
-
%x{mkdir "#{dest_path}"}
|
91
|
-
rescue Error
|
92
|
-
puts "Cant't create new directory!"
|
93
|
-
end
|
94
|
-
|
95
|
-
Dir.chdir(base_dir_path + "/" + source_project_name)
|
96
|
-
copy_files(dest_path, ignored_files)
|
97
|
-
Dir.chdir(dest_path)
|
98
|
-
|
99
|
-
old_app_name = source_project_name.camelize
|
100
|
-
new_app_name = dest_project_name.camelize
|
101
|
-
|
102
|
-
# Are we inside an engine? If so, we have to rename some files and folders
|
103
|
-
if File.exists?("lib/#{source_project_name}/engine.rb")
|
104
|
-
rename_file_objects(source_project_name, dest_project_name) do |old_paths|
|
105
|
-
old_paths.keep_if { |path| File.directory?(path) }
|
106
|
-
end
|
7
|
+
ignored_files = ARGV[2..-1]
|
107
8
|
|
108
|
-
|
109
|
-
|
110
|
-
end
|
9
|
+
forker = ForkRailsProject::Forker.new(source_project_name, dest_project_name, ignored_files)
|
10
|
+
forker.fork!
|
111
11
|
|
112
|
-
substitute_names(source_project_name, dest_project_name)
|
113
|
-
puts
|
114
|
-
substitute_names(old_app_name, new_app_name)
|
data/lib/forker.rb
ADDED
@@ -0,0 +1,133 @@
|
|
1
|
+
require "active_support/all"
|
2
|
+
require "open3"
|
3
|
+
require "find"
|
4
|
+
|
5
|
+
module ForkRailsProject
|
6
|
+
class Forker
|
7
|
+
|
8
|
+
attr_accessor :source_project_name, :dest_project_name, :ignored_files
|
9
|
+
|
10
|
+
def initialize(source_project_name, dest_project_name, ignored_files = [])
|
11
|
+
@source_project_name = source_project_name
|
12
|
+
@dest_project_name = dest_project_name
|
13
|
+
@ignored_files = ignored_files
|
14
|
+
folder_names = [] << @source_project_name << @dest_project_name
|
15
|
+
|
16
|
+
folder_names.each do |a|
|
17
|
+
a.strip!
|
18
|
+
if a.start_with?("/")
|
19
|
+
a.slice!(0)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
if !File.directory?("./#{@source_project_name}")
|
24
|
+
raise RuntimeError, "#{@source_project_name} is not a valid dir!"
|
25
|
+
end
|
26
|
+
|
27
|
+
if File.directory?("./#{@dest_project_name}")
|
28
|
+
raise RuntimeError, "Destination directory already exists!"
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def fork!
|
33
|
+
base_dir_path = Dir.pwd
|
34
|
+
dest_path = [base_dir_path, "/", @dest_project_name].join
|
35
|
+
|
36
|
+
begin
|
37
|
+
%x[mkdir "#{dest_path}"]
|
38
|
+
rescue Error
|
39
|
+
puts "Cant't create new directory!"
|
40
|
+
end
|
41
|
+
|
42
|
+
Dir.chdir([base_dir_path, "/", @source_project_name].join)
|
43
|
+
copy_files(dest_path, @ignored_files)
|
44
|
+
Dir.chdir(dest_path)
|
45
|
+
|
46
|
+
old_app_name = @source_project_name.camelize
|
47
|
+
new_app_name = @dest_project_name.camelize
|
48
|
+
|
49
|
+
# Are we inside an engine? If so, we have to rename some files and folders:
|
50
|
+
if File.exists?("lib/#{@source_project_name}/engine.rb")
|
51
|
+
# first step: copy and rename directories
|
52
|
+
rename_file_objects(@source_project_name, @dest_project_name) do |old_paths|
|
53
|
+
old_paths.keep_if { |path| File.directory?(path) }
|
54
|
+
end
|
55
|
+
|
56
|
+
# called without above block: copy and rename files in engine
|
57
|
+
rename_file_objects(@source_project_name, @dest_project_name)
|
58
|
+
puts
|
59
|
+
end
|
60
|
+
|
61
|
+
# alter snake_cased strings
|
62
|
+
substitute_names(@source_project_name, dest_project_name)
|
63
|
+
puts
|
64
|
+
# alter CamelCased strings
|
65
|
+
substitute_names(old_app_name, new_app_name)
|
66
|
+
end
|
67
|
+
|
68
|
+
private
|
69
|
+
|
70
|
+
def copy_files(dest_path, ignored_files)
|
71
|
+
copy_string = "rsync -ax "
|
72
|
+
ignored_files.each do |file_name|
|
73
|
+
copy_string << "--exclude #{file_name} "
|
74
|
+
end
|
75
|
+
copy_string << ". #{dest_path}"
|
76
|
+
begin
|
77
|
+
puts "Copying files from #{Dir.pwd} to #{dest_path}"
|
78
|
+
puts "Ignored files/directories: #{ignored_files.join(' ')}"
|
79
|
+
puts "Executing #{copy_string}"
|
80
|
+
%x[#{copy_string}]
|
81
|
+
puts
|
82
|
+
rescue IOError
|
83
|
+
puts "Unable to copy files!"
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
def rename_file_objects(old_name, new_name)
|
88
|
+
old_file_paths = Find.find(".").flat_map do |path|
|
89
|
+
path if path =~ /#{old_name}/
|
90
|
+
end.compact
|
91
|
+
old_file_paths = yield(old_file_paths) if block_given?
|
92
|
+
files_to_move = old_file_paths.inject({}) do |hash, path|
|
93
|
+
new_path = path.gsub(/#{old_name}/, new_name)
|
94
|
+
hash[path] = new_path
|
95
|
+
hash
|
96
|
+
end
|
97
|
+
files_to_move.each do |old, new|
|
98
|
+
puts "Renaming #{old} -> #{new}"
|
99
|
+
# we don't want 'no such file' output for duplicate paths
|
100
|
+
stdin, stdout, stderr = Open3.popen3("mv #{old} #{new}")
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
def substitute_names(old_name, new_name)
|
105
|
+
puts "Replacing string '#{old_name}' in application files..."
|
106
|
+
occurrences = %x[grep -iR "#{old_name}" --exclude-dir=log --exclude-dir=tmp --exclude=tags .]
|
107
|
+
occurrences = occurrences.split("\n")
|
108
|
+
files = occurrences.each.flat_map do |occ|
|
109
|
+
occ.slice!(0..1)
|
110
|
+
occ.slice(0...occ.index(":")) if occ.include?(":")
|
111
|
+
end.uniq.compact
|
112
|
+
|
113
|
+
begin
|
114
|
+
files.each do |file|
|
115
|
+
if File.exist?(file)
|
116
|
+
text = File.read(file)
|
117
|
+
text = text.gsub(old_name, new_name)
|
118
|
+
File.open(file, "w+") { |line| line.puts text }
|
119
|
+
end
|
120
|
+
end
|
121
|
+
rescue IOError
|
122
|
+
puts "Unable to alter new files!"
|
123
|
+
end
|
124
|
+
|
125
|
+
puts "Finished replacing string '#{old_name}' in application files."\
|
126
|
+
" Altered files:"
|
127
|
+
files.each do |filename|
|
128
|
+
puts filename
|
129
|
+
end
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fork_rails_project
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alexander Huber
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-10-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -24,6 +24,34 @@ dependencies:
|
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '4'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 3.1.0
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 3.1.0
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: pry
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 0.10.1
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 0.10.1
|
27
55
|
description: Generates copies of existing app directories with automatic renaming/namespacing
|
28
56
|
of contained files
|
29
57
|
email: alih83@gmx.de
|
@@ -34,6 +62,7 @@ extra_rdoc_files: []
|
|
34
62
|
files:
|
35
63
|
- bin/fork-rails-project
|
36
64
|
- lib/fork_rails_project.rb
|
65
|
+
- lib/forker.rb
|
37
66
|
homepage: https://github.com/alihuber/fork_rails_project
|
38
67
|
licenses:
|
39
68
|
- MIT
|
@@ -54,7 +83,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
54
83
|
version: '0'
|
55
84
|
requirements: []
|
56
85
|
rubyforge_project:
|
57
|
-
rubygems_version: 2.
|
86
|
+
rubygems_version: 2.4.2
|
58
87
|
signing_key:
|
59
88
|
specification_version: 4
|
60
89
|
summary: Script to set up forks of existing rails apps/engines
|