Permalink
Browse files

First commit

  • Loading branch information...
1 parent 4ab38af commit 534cb7417c3e31cd4041a7085b99ced4ba2e58a2 @winton committed Nov 7, 2009
View
20 README.markdown
@@ -1,18 +1,4 @@
-GemTemplate
-===========
+Stencil
+=======
-A gem template for new projects.
-
-Setup
------
-
-<pre>
-git clone git://github.com/winton/gem_template.git my_project
-cd my_project
-rake setup
-git remote add origin git@github.com:winton/my_project.git
-</pre>
-
-A project wide find-replace occurs on file names and contents.
-
-Edit <code>gemspec.rb</code> and <code>MIT-LICENSE</code>, and your project is ready for its first commit.
+Project template manager.
View
22 Rakefile
@@ -24,28 +24,6 @@ Rake::GemPackageTask.new(GEM_SPEC) do |pkg|
pkg.gem_spec = GEM_SPEC
end
-desc "Setup project"
-task :setup do
- name = File.basename(Dir.pwd)
- `rm -Rf .git`
- begin
- dir = Dir['**/gem_template*']
- from = dir.pop
- if from
- rb = from.include?('.rb')
- to = File.dirname(from) + "/#{name}#{'.rb' if rb}"
- FileUtils.mv(from, to)
- end
- end while dir.length > 0
- Dir["**/*"].each do |path|
- next if path.include?('Rakefile')
- if File.file?(path)
- `sed -i "" 's/gem_template/#{name}/g' #{path}`
- end
- end
- `git init`
-end
-
desc "Run specs"
Spec::Rake::SpecTask.new do |t|
t.rcov = true
View
4 bin/gem_template → bin/stencil
@@ -1,4 +1,6 @@
#!/usr/bin/env ruby
$:.push File.expand_path("#{File.dirname(__FILE__)}/../lib")
-require 'gem_template'
+require 'stencil'
+
+Stencil.new(ARGV)
View
8 gemspec.rb
@@ -1,19 +1,19 @@
-GEM_NAME = 'gem_template'
+GEM_NAME = 'stencil'
GEM_FILES = FileList['**/*'] - FileList['coverage', 'coverage/**/*', 'pkg', 'pkg/**/*']
GEM_SPEC = Gem::Specification.new do |s|
# == CONFIGURE ==
s.author = "Winton Welsh"
s.email = "mail@wintoni.us"
s.homepage = "http://github.com/winton/#{GEM_NAME}"
- s.summary = ""
+ s.summary = "Project template manager"
# == CONFIGURE ==
+ s.add_dependency('httparty', '=0.4.5')
s.executables << GEM_NAME
- s.extensions << "install"
s.extra_rdoc_files = [ "README.markdown" ]
s.files = GEM_FILES.to_a
s.has_rdoc = false
s.name = GEM_NAME
s.platform = Gem::Platform::RUBY
s.require_path = "lib"
s.version = "0.1.0"
-end
+end
View
4 install
@@ -1,4 +0,0 @@
-#!/usr/bin/env ruby
-
-$:.push File.expand_path("#{File.dirname(__FILE__)}/lib")
-require 'gem_template'
View
1 lib/gem_template.rb
@@ -1 +0,0 @@
-require File.dirname(__FILE__) + "/gem_template/gem_template"
View
0 lib/gem_template/gem_template.rb
No changes.
View
50 lib/stencil.rb
@@ -0,0 +1,50 @@
+Dir["#{File.dirname(__FILE__)}/stencil/*.rb"].each do |path|
+ require path
+end
+
+class Stencil
+
+ def initialize(args)
+ path = Dir.pwd
+ name = File.basename(path).intern
+
+ # If template, do a template merge
+ if Config.exists?(:templates, path)
+ Merge.template(path)
+
+ # If project
+ elsif Config.exists?(:projects, path)
+
+ # If upstream commit, merge upstream
+ if args.first == '<'
+ Merge.upstream args[1..-1]
+
+ # If template specified, update config
+ elsif args.first
+ Config.update(:projects => {
+ name => {
+ :template => args.shift,
+ :branches => args
+ }
+ })
+
+ end
+
+ # Do a project merge
+ Merge.project(name, path)
+
+ # If not configured
+ else
+
+ # Update config
+ Msg.is_template_or_project?(name)
+ Config.update((STDIN.gets[0..0].downcase == 't' ? :templates : :projects) => {
+ name => { :path => path }
+ })
+
+ # Re-run
+ initialize args
+
+ end
+ end
+end
View
35 lib/stencil/branches.rb
@@ -0,0 +1,35 @@
+require File.dirname(__FILE__) + "/cmd"
+
+class Stencil
+ class Branches
+ class <<self
+
+ def read(path)
+ branches = Cmd.run path, 'git branch'
+ branches = branches.split(/\s+/)
+ branches.delete '*'
+ branches.delete 'master'
+ branches
+ end
+
+ def grouped(path)
+ groups, ignore = [], []
+ branches = read(path).sort { |a, b| a.length <=> b.length }
+ branches.each do |branch|
+ next if ignore.include?(branch)
+ groups << [ branch ] + group(branches, branch)
+ ignore += groups.last
+ end
+ groups
+ end
+
+ private
+
+ def group(branches, branch)
+ branches.select do |b|
+ b != branch && b[0..branch.length-1] == branch
+ end
+ end
+ end
+ end
+end
View
15 lib/stencil/cmd.rb
@@ -0,0 +1,15 @@
+class Stencil
+ class Cmd
+ class <<self
+
+ def run(path, cmd=nil)
+ if cmd.nil?
+ cmd, path = path, cmd
+ else
+ path = "cd #{path} && "
+ end
+ `#{[ path, cmd ].compact.join}`
+ end
+ end
+ end
+end
View
57 lib/stencil/config.rb
@@ -0,0 +1,57 @@
+require 'yaml'
+
+class Stencil
+ class Config
+ class <<self
+
+ @@cache = nil
+ @@path = File.expand_path('~/.stencil.yml')
+
+ def create
+ write(:projects => {}, :templates => {})
+ end
+
+ def read
+ if @@cache
+ @@cache
+ elsif File.exists?(@@path)
+ File.open(@@path, 'r') do |f|
+ @@cache = YAML::load f
+ end
+ else
+ create
+ @@cache = read
+ end
+ end
+
+ def update(hash)
+ write read.deep_merge(hash)
+ end
+
+ def delete
+ @@cache = nil
+ File.unlink @@path
+ end
+
+ def exists?(type, name)
+ read[type] &&
+ (
+ read[type][name.intern] ||
+ (
+ read[type][File.basename(name).intern] &&
+ read[type][File.basename(name).intern][:path] == name
+ )
+ )
+ end
+
+ private
+
+ def write(hash)
+ @@cache = nil
+ File.open(@@path, 'w') do |f|
+ f.write YAML::dump(hash)
+ end
+ end
+ end
+ end
+end
View
6 lib/stencil/hash.rb
@@ -0,0 +1,6 @@
+class Hash
+ def deep_merge(second)
+ merger = proc { |key, v1, v2| Hash === v1 && Hash === v2 ? v1.merge(v2, &merger) : v2 }
+ self.merge(second, &merger)
+ end
+end
View
55 lib/stencil/merge.rb
@@ -0,0 +1,55 @@
+class Stencil
+ class Merge
+ class <<self
+
+ def project(name, path)
+ template = Config.read[:projects][name][:template]
+ branches = Config.read[:projects][name][:branches]
+ if template
+ template = Config.read[:templates][template.intern]
+ if template && File.exists?(template[:path])
+ origin = Cmd.run template[:path], "git remote show origin"
+ origin = origin.match(/URL:\s+(\S+)/)[1]
+ Msg.template_url origin
+ Cmd.run path, "git remote rm template"
+ Cmd.run path, "git remote add template #{origin}"
+ branches = %w(master) if branches.empty?
+ branches.each do |branch|
+ Msg.merge_remote_branch branch
+ Cmd.run path, "git pull template #{branch}"
+ end
+ else
+ Msg.template_not_found template
+ Msg.specify_template
+ end
+ else
+ Msg.specify_template
+ end
+ end
+
+ def template(path)
+ Branches.grouped(path).each do |branches|
+ branches.unshift('master')
+ progressive(path, branches)
+ end
+ Cmd.run path, "git checkout master"
+ end
+
+ private
+
+ def progressive(path, branches)
+ merger = branches.shift
+ mergee = branches.first
+ if merger && mergee
+ puts "Merging \"#{merger}\" into \"#{mergee}\""
+ output = Cmd.run(path, "git checkout #{mergee} && git merge #{merger}")
+ if output.downcase.include?('conflict')
+ puts output
+ else
+ progressive(path, branches)
+ end
+ end
+ end
+ end
+ end
+end
View
27 lib/stencil/msg.rb
@@ -0,0 +1,27 @@
+class Stencil
+ class Msg
+ class <<self
+
+ def is_template_or_project?(name)
+ puts "Is \"#{name}\" a template or a project?"
+ end
+
+ def merge_remote_branch(branch)
+ puts "Merging remote branch \"#{branch}\""
+ end
+
+ def specify_template
+ puts "Please tell stencil what template you want to receive updates from:"
+ puts " stencil TEMPLATE [BRANCH BRANCH ...]"
+ end
+
+ def template_not_found(template)
+ puts "Template \"#{template}\" not found."
+ end
+
+ def template_url(url)
+ puts "Found template URL: #{url}"
+ end
+ end
+ end
+end
View
2 spec/spec_helper.rb
@@ -2,7 +2,7 @@
SPEC = File.dirname(__FILE__)
$:.unshift File.expand_path("#{SPEC}/../lib")
-require 'gem_template'
+require 'stencil'
require 'pp'
Spec::Runner.configure do |config|
View
32 stencil.gemspec
@@ -0,0 +1,32 @@
+# -*- encoding: utf-8 -*-
+
+Gem::Specification.new do |s|
+ s.name = %q{stencil}
+ s.version = "0.1.0"
+
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
+ s.authors = ["Winton Welsh"]
+ s.date = %q{2009-11-06}
+ s.default_executable = %q{stencil}
+ s.email = %q{mail@wintoni.us}
+ s.executables = ["stencil"]
+ s.extra_rdoc_files = ["README.markdown"]
+ s.files = ["bin", "bin/stencil", "gemspec.rb", "lib", "lib/stencil", "lib/stencil/branches.rb", "lib/stencil/cmd.rb", "lib/stencil/config.rb", "lib/stencil/hash.rb", "lib/stencil/merge.rb", "lib/stencil/msg.rb", "lib/stencil.rb", "MIT-LICENSE", "Rakefile", "README.markdown", "spec", "spec/spec.opts", "spec/spec_helper.rb"]
+ s.homepage = %q{http://github.com/winton/stencil}
+ s.require_paths = ["lib"]
+ s.rubygems_version = %q{1.3.5}
+ s.summary = %q{Project template manager}
+
+ if s.respond_to? :specification_version then
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
+ s.specification_version = 3
+
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
+ s.add_runtime_dependency(%q<httparty>, ["= 0.4.5"])
+ else
+ s.add_dependency(%q<httparty>, ["= 0.4.5"])
+ end
+ else
+ s.add_dependency(%q<httparty>, ["= 0.4.5"])
+ end
+end

0 comments on commit 534cb74

Please sign in to comment.