41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
# File 'lib/rubypitaya.rb', line 41
def self.add_plugin(plugin_git_url, branch_name)
Dir.mkdir(Path::PLUGINS_FOLDER_PATH) unless File.exists?(Path::PLUGINS_FOLDER_PATH)
plugin_name = plugin_git_url.scan(/.+\/(.+)\.git/).flatten.first
plugin_folder_path = File.join(Path::PLUGINS_FOLDER_PATH, plugin_name)
plugin_git_path = File.join(plugin_folder_path, '.git/')
commit_hash = ''
branch_command = ''
branch_command = "--branch #{branch_name}" unless branch_name.blank?
FileUtils.rm_rf(plugin_folder_path) if File.exists?(plugin_folder_path)
puts "git -C #{Path::PLUGINS_FOLDER_PATH} clone --depth 1 #{branch_command} #{plugin_git_url}"
`git -C #{Path::PLUGINS_FOLDER_PATH} clone --depth 1 #{branch_command} #{plugin_git_url}`
commit_hash = `git rev-parse --short HEAD`.strip
FileUtils.rm_rf(plugin_git_path)
Dir.entries(plugin_folder_path).each do |entry|
entry_path = File.join(plugin_folder_path, entry)
FileUtils.rm_rf(entry_path) unless entry == 'app' ||
entry == 'spec' ||
entry == 'features' ||
entry == '.' ||
entry == '..'
end
plugin_migrations_path = File.join(plugin_folder_path, 'app/migrations/')
plugin_migrations_files = Dir[File.join(plugin_migrations_path, '*')]
base_migration_timestamp = Time.now.utc.to_i
plugin_migrations_files.each_with_index do |migration_file, i|
migration_timestamp = base_migration_timestamp + i
new_file = migration_file.gsub(/^(.+\/migrations\/)\d+(_.+)$/, "\\1#{migration_timestamp}\\2")
File.rename(migration_file, new_file)
end
plugins_config = YAML.load(File.open(Path::PLUGIN_CONFIG_FILE_PATH, &:read))
plugins_config['plugins'][plugin_name] = {
'git' => plugin_git_url,
'branch' => branch_name || '',
'commit' => commit_hash,
}
File.open(Path::PLUGIN_CONFIG_FILE_PATH, 'w') { |f| f.write(plugins_config.to_yaml) }
plugin_name
end
|