Skip to content

Instantly share code, notes, and snippets.

@insoul
Last active December 28, 2015 07:39
Show Gist options
  • Save insoul/7466011 to your computer and use it in GitHub Desktop.
Save insoul/7466011 to your computer and use it in GitHub Desktop.
Make Sublime Text project file using ruby bundler. Project file will be created in upper folder. Check `subp --help`
#!/usr/bin/env ruby
require 'json'
require 'optparse'
# variables
project_name = Dir.pwd.split('/').last
project_file = "../.#{project_name}.sublime-project"
puts "Project file is #{project_file}"
# gem attributes
class PathEntry
attr_writer :lib_path
def initialize(lib_path)
@lib_path = lib_path
end
def name
path.split('/').last
end
def path
@path ||= begin
if is_gem?
splited = @lib_path.split('/')
splited[0..splited.rindex('gems')+1].join('/')
else
@lib_path
end
end
end
def is_gem?
@lib_path =~ /gems/
end
def is_current_subdir?
@lib_path =~ /^#{Dir.pwd}.+/
end
end
# parse subcommand and options
options = {
:subl_command => 'subl'
}
subcommands = {
'make' => OptionParser.new do |opts|
opts.banner = "Usage: make [options]"
opts.on('-f', '--force-update', 'Force update all settings') do |f|
options[:force] = f
end
opts.on_tail('-h', '--help', 'Show this message') do
puts opts
exit
end
end,
'open' => OptionParser.new do |opts|
opts.banner = "Usage: open [options]"
opts.on('-c', '--subl-command [command]', 'Sublime Text command') do |c|
options[:subl_command] = 'subl'
end
opts.on_tail('-h', '--help', 'Show this message') do
puts opts
exit
end
end
}
global = OptionParser.new do |opts|
opts.banner = "Usage: subp [options] [subcommand [options]]"
opts.on_tail('-h', '--help', 'Show this message') do
puts opts
puts " Subcommands: #{subcommands.keys.inspect}"
exit
end
end
make = Proc.new do
unless system('bundle check')
raise 'You must run firstly bundle install'
end
gem_list = `cd #{Dir.pwd}; bundle exec ruby -e 'puts $:'`
gem_lib_paths = gem_list.split("\n")
path_entries = gem_lib_paths.map { |lib_path| PathEntry.new(lib_path) }
path_entries.delete_if { |entry|
!entry.is_gem? or entry.is_current_subdir?
}
path_entries.unshift(PathEntry.new(Dir.pwd))
puts "Project folders"
puts path_entries.map{|entry| entry.path}
# backup existing file
if File.exists?(project_file)
puts "Backup to #{project_file}.bak"
`cd #{Dir.pwd}; cp -f #{project_file} #{project_file}.bak`
end
# read and update sublime project file
if options[:force] or not File.exists?(project_file)
puts "Reset #{project_file}"
sublime = {'folders'=>[]}
else
puts "Read #{project_file}"
sublime = JSON.parse(File.read(project_file))
end
sublime['folders'].delete_if do |folder|
sublime_folders_found_in_bundle_path_entries = path_entries.index { |entry| entry.path == folder['path'] }
if sublime_folders_found_in_bundle_path_entries
false
else
puts "Delete #{folder['path']}"
true
end
end
puts
path_entries.each do |entry|
bundle_path_entry_included_in_sublime = sublime['folders'].index { |folder| folder['path'] == entry.path }
unless bundle_path_entry_included_in_sublime
puts "Add #{entry.path}"
sublime['folders'].push('path' => entry.path, 'name' => entry.name, 'follow_symlinks' => true)
end
end
sublime['folders'] = sublime['folders'].sort_by { |folder| folder['path'] }
if i = sublime['folders'].index { |folder| folder['path'] == Dir.pwd }
puts "Move current directory to top"
sublime['folders'].unshift(sublime['folders'].delete_at(i))
else
puts "Add current directory"
sublime['folders'].unshift('path' => Dir.pwd, 'follow_symlinks' => true)
end
File.open(project_file, 'w') {|f| f.write JSON.pretty_generate(sublime)}
puts
puts "Write #{project_file}"
end
global.order!
subcommand = ARGV.shift || 'open'
subcommands[subcommand].order!
puts "subcommand: #{subcommand}"
puts "options: #{options.inspect}"
# run command
if subcommand == 'open'
if File.exists?(project_file)
system("#{options[:subl_command]} #{project_file}")
exit 0
else
instance_eval(&make)
system("#{options[:subl_command]} #{project_file}")
exit 0
end
elsif subcommand == 'make'
instance_eval(&make)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment