2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-11-08 01:48:23 +08:00
|
|
|
directory 'plugins'
|
|
|
|
|
2017-07-19 04:55:31 +08:00
|
|
|
desc 'install all official plugins (use GIT_WRITE=1 to pull with write access)'
|
2017-07-19 00:09:27 +08:00
|
|
|
task 'plugin:install_all_official' do
|
|
|
|
skip = Set.new([
|
|
|
|
'customer-flair',
|
2019-08-21 17:02:55 +08:00
|
|
|
'lazy-yt',
|
2019-05-24 17:35:22 +08:00
|
|
|
'poll'
|
2017-07-19 00:09:27 +08:00
|
|
|
])
|
|
|
|
|
|
|
|
map = {
|
2020-10-15 03:05:10 +08:00
|
|
|
'Canned Replies' => 'https://github.com/discourse/discourse-canned-replies'
|
2017-07-19 00:09:27 +08:00
|
|
|
}
|
|
|
|
|
2019-07-30 21:53:56 +08:00
|
|
|
STDERR.puts "Allowing write to all repos!" if ENV['GIT_WRITE']
|
|
|
|
|
2017-07-19 00:09:27 +08:00
|
|
|
Plugin::Metadata::OFFICIAL_PLUGINS.each do |name|
|
|
|
|
next if skip.include? name
|
|
|
|
repo = map[name] || "https://github.com/discourse/#{name}"
|
|
|
|
dir = repo.split('/').last
|
|
|
|
path = File.expand_path('plugins/' + dir)
|
|
|
|
|
|
|
|
if Dir.exist? path
|
|
|
|
STDERR.puts "Skipping #{dir} cause it already exists!"
|
|
|
|
next
|
|
|
|
end
|
|
|
|
|
|
|
|
if ENV['GIT_WRITE']
|
2019-05-13 10:18:53 +08:00
|
|
|
repo = repo.gsub("https://github.com/", "git@github.com:")
|
|
|
|
repo += ".git"
|
2017-07-19 00:09:27 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
status = system("git clone #{repo} #{path}")
|
|
|
|
unless status
|
|
|
|
abort("Failed to clone #{repo}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-11-08 01:48:23 +08:00
|
|
|
desc 'install plugin'
|
|
|
|
task 'plugin:install', :repo do |t, args|
|
|
|
|
repo = ENV['REPO'] || ENV['repo'] || args[:repo]
|
|
|
|
name = ENV['NAME'] || ENV['name'] || File.basename(repo, '.git')
|
|
|
|
|
|
|
|
plugin_path = File.expand_path('plugins/' + name)
|
|
|
|
if File.directory?(File.expand_path(plugin_path))
|
|
|
|
abort('Plugin directory, ' + plugin_path + ', already exists.')
|
|
|
|
end
|
|
|
|
|
|
|
|
clone_status = system('git clone ' + repo + ' ' + plugin_path)
|
|
|
|
unless clone_status
|
|
|
|
FileUtils.rm_rf(plugin_path)
|
|
|
|
abort('Unable to clone plugin')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
desc 'update all plugins'
|
|
|
|
task 'plugin:update_all' do |t|
|
|
|
|
# Loop through each directory
|
|
|
|
plugins = Dir.glob(File.expand_path('plugins/*')).select { |f| File.directory? f }
|
|
|
|
# run plugin:update
|
2019-06-13 05:21:56 +08:00
|
|
|
plugins.each do |plugin|
|
|
|
|
next unless File.directory?(plugin + "/.git")
|
|
|
|
Rake::Task['plugin:update'].invoke(plugin)
|
|
|
|
Rake::Task['plugin:update'].reenable
|
|
|
|
end
|
2020-09-01 23:05:07 +08:00
|
|
|
Rake::Task['plugin:versions'].invoke
|
2013-11-08 01:48:23 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
desc 'update a plugin'
|
|
|
|
task 'plugin:update', :plugin do |t, args|
|
|
|
|
plugin = ENV['PLUGIN'] || ENV['plugin'] || args[:plugin]
|
|
|
|
plugin_path = plugin
|
|
|
|
plugin = File.basename(plugin)
|
|
|
|
|
|
|
|
unless File.directory?(plugin_path)
|
|
|
|
if File.directory?('plugins/' + plugin)
|
|
|
|
plugin_path = File.expand_path('plugins/' + plugin)
|
|
|
|
else
|
|
|
|
abort('Plugin ' + plugin + ' not found')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-02-11 00:34:17 +08:00
|
|
|
`git -C '#{plugin_path}' fetch origin --tags --force`
|
|
|
|
|
|
|
|
upstream_branch = `git -C '#{plugin_path}' for-each-ref --format='%(upstream:short)' $(git -C '#{plugin_path}' symbolic-ref -q HEAD)`.strip
|
|
|
|
has_origin_main = `git -C '#{plugin_path}' branch -a`.match?(/remotes\/origin\/main$/)
|
|
|
|
has_local_main = `git -C '#{plugin_path}' show-ref refs/heads/main`.present?
|
|
|
|
|
|
|
|
if upstream_branch == "origin/master" && has_origin_main
|
|
|
|
puts "Branch has changed to `origin/main`"
|
|
|
|
|
|
|
|
if has_local_main
|
|
|
|
update_status = system("git -C '#{plugin_path}' checkout main")
|
2021-02-17 03:50:27 +08:00
|
|
|
abort("Unable to pull latest version of plugin #{plugin_path}") unless update_status
|
2021-02-11 00:34:17 +08:00
|
|
|
else
|
|
|
|
`git -C '#{plugin_path}' branch -m master main`
|
|
|
|
end
|
|
|
|
|
|
|
|
`git -C '#{plugin_path}' branch -u origin/main main`
|
|
|
|
end
|
|
|
|
|
2022-03-14 22:13:06 +08:00
|
|
|
update_status = system("git -C '#{plugin_path}' pull --no-rebase")
|
2021-02-17 03:50:27 +08:00
|
|
|
abort("Unable to pull latest version of plugin #{plugin_path}") unless update_status
|
2013-11-08 01:48:23 +08:00
|
|
|
end
|
2014-02-02 04:22:53 +08:00
|
|
|
|
2020-07-07 05:48:00 +08:00
|
|
|
desc 'pull compatible plugin versions for all plugins'
|
|
|
|
task 'plugin:pull_compatible_all' do |t|
|
2022-01-11 20:30:22 +08:00
|
|
|
if GlobalSetting.load_plugins?
|
|
|
|
STDERR.puts <<~TEXT
|
|
|
|
WARNING: Plugins were activated before running `rake plugin:pull_compatible_all`
|
|
|
|
You should prefix this command with LOAD_PLUGINS=0
|
|
|
|
TEXT
|
|
|
|
end
|
|
|
|
|
2020-07-07 05:48:00 +08:00
|
|
|
# Loop through each directory
|
|
|
|
plugins = Dir.glob(File.expand_path('plugins/*')).select { |f| File.directory? f }
|
|
|
|
# run plugin:pull_compatible
|
|
|
|
plugins.each do |plugin|
|
|
|
|
next unless File.directory?(plugin + "/.git")
|
|
|
|
Rake::Task['plugin:pull_compatible'].invoke(plugin)
|
|
|
|
Rake::Task['plugin:pull_compatible'].reenable
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
desc 'pull a compatible plugin version'
|
|
|
|
task 'plugin:pull_compatible', :plugin do |t, args|
|
|
|
|
plugin = ENV['PLUGIN'] || ENV['plugin'] || args[:plugin]
|
|
|
|
plugin_path = plugin
|
|
|
|
plugin = File.basename(plugin)
|
|
|
|
|
|
|
|
unless File.directory?(plugin_path)
|
|
|
|
if File.directory?('plugins/' + plugin)
|
|
|
|
plugin_path = File.expand_path('plugins/' + plugin)
|
|
|
|
else
|
|
|
|
abort('Plugin ' + plugin + ' not found')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
checkout_version = Discourse.find_compatible_git_resource(plugin_path)
|
|
|
|
|
|
|
|
# Checkout value of the version compat
|
|
|
|
if checkout_version
|
|
|
|
puts "checking out compatible #{plugin} version: #{checkout_version}"
|
2020-08-13 07:55:49 +08:00
|
|
|
update_status = system("git -C '#{plugin_path}' cat-file -e #{checkout_version} || git -C '#{plugin_path}' fetch --depth 1 $(git -C '#{plugin_path}' rev-parse --symbolic-full-name @{upstream} | awk -F '/' '{print $3}') #{checkout_version}; git -C '#{plugin_path}' reset --hard #{checkout_version}")
|
2020-07-07 05:48:00 +08:00
|
|
|
abort('Unable to checkout a compatible plugin version') unless update_status
|
|
|
|
else
|
|
|
|
puts "#{plugin} is already at latest compatible version"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-06-13 21:58:57 +08:00
|
|
|
desc 'install all plugin gems'
|
|
|
|
task 'plugin:install_all_gems' do |t|
|
2022-03-15 22:08:05 +08:00
|
|
|
# Left intentionally blank.
|
|
|
|
# When the app is being loaded, all missing gems are installed
|
|
|
|
# See: lib/plugin_gem.rb
|
|
|
|
puts "Done"
|
2019-06-13 21:58:57 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
desc 'install plugin gems'
|
|
|
|
task 'plugin:install_gems', :plugin do |t, args|
|
2022-03-15 22:08:05 +08:00
|
|
|
# Left intentionally blank.
|
|
|
|
# When the app is being loaded, all missing gems are installed
|
|
|
|
# See: lib/plugin_gem.rb
|
|
|
|
puts "Done"
|
2019-06-13 21:58:57 +08:00
|
|
|
end
|
|
|
|
|
2022-09-20 22:42:54 +08:00
|
|
|
def spec(plugin, parallel: false)
|
|
|
|
params = []
|
|
|
|
params << '--profile' if !parallel
|
2022-08-03 10:52:53 +08:00
|
|
|
params << '--fail-fast' if ENV['RSPEC_FAILFAST']
|
|
|
|
params << "--seed #{ENV['RSPEC_SEED']}" if Integer(ENV['RSPEC_SEED'], exception: false)
|
|
|
|
|
2014-02-02 04:22:53 +08:00
|
|
|
ruby = `which ruby`.strip
|
2022-11-04 22:06:24 +08:00
|
|
|
# reject system specs as they are slow and need dedicated setup
|
|
|
|
files =
|
|
|
|
Dir.glob("./plugins/#{plugin}/spec/**/*_spec.rb").reject { |f| f.include?("spec/system/") }.sort
|
2014-02-02 04:22:53 +08:00
|
|
|
if files.length > 0
|
2022-09-20 22:42:54 +08:00
|
|
|
cmd = parallel ? "bin/turbo_rspec" : "bin/rspec"
|
|
|
|
sh "LOAD_PLUGINS=1 #{cmd} #{files.join(' ')} #{params.join(' ')}"
|
2014-02-02 04:22:53 +08:00
|
|
|
else
|
|
|
|
abort "No specs found."
|
|
|
|
end
|
|
|
|
end
|
2017-07-26 21:07:46 +08:00
|
|
|
|
2022-09-20 22:42:54 +08:00
|
|
|
desc 'run plugin specs'
|
|
|
|
task 'plugin:spec', :plugin do |t, args|
|
|
|
|
args.with_defaults(plugin: "*")
|
|
|
|
spec(args[:plugin])
|
|
|
|
end
|
|
|
|
|
|
|
|
desc 'run plugin specs in parallel'
|
|
|
|
task 'plugin:turbo_spec', :plugin do |t, args|
|
|
|
|
args.with_defaults(plugin: "*")
|
|
|
|
spec(args[:plugin], parallel: true)
|
|
|
|
end
|
|
|
|
|
2017-07-26 21:07:46 +08:00
|
|
|
desc 'run plugin qunit tests'
|
|
|
|
task 'plugin:qunit', [:plugin, :timeout] do |t, args|
|
|
|
|
args.with_defaults(plugin: "*")
|
|
|
|
|
2022-01-12 03:03:08 +08:00
|
|
|
rake = "#{Rails.root}/bin/rake"
|
2017-07-27 00:54:56 +08:00
|
|
|
|
|
|
|
cmd = 'LOAD_PLUGINS=1 '
|
|
|
|
cmd += 'QUNIT_SKIP_CORE=1 '
|
2017-07-27 01:11:08 +08:00
|
|
|
|
2017-07-26 21:07:46 +08:00
|
|
|
if args[:plugin] == "*"
|
|
|
|
puts "Running qunit tests for all plugins"
|
|
|
|
else
|
|
|
|
puts "Running qunit tests for #{args[:plugin]}"
|
2017-07-27 00:54:56 +08:00
|
|
|
cmd += "QUNIT_SINGLE_PLUGIN='#{args[:plugin]}' "
|
2017-07-26 21:07:46 +08:00
|
|
|
end
|
2017-07-27 01:11:08 +08:00
|
|
|
|
2017-07-27 00:54:56 +08:00
|
|
|
cmd += "#{rake} qunit:test"
|
2017-07-27 01:11:08 +08:00
|
|
|
cmd += "[#{args[:timeout]}]" if args[:timeout]
|
2017-07-27 00:54:56 +08:00
|
|
|
|
2022-01-12 03:03:08 +08:00
|
|
|
system cmd
|
|
|
|
exit $?.exitstatus
|
2017-07-26 21:07:46 +08:00
|
|
|
end
|
2020-03-20 02:30:08 +08:00
|
|
|
|
|
|
|
desc 'run all migrations of a plugin'
|
|
|
|
namespace 'plugin:migrate' do
|
|
|
|
def list_migrations(plugin_name)
|
|
|
|
plugin_root = File.join(Rails.root, "plugins", plugin_name)
|
|
|
|
migrations_root = File.join(plugin_root, "db", "{post_migrate,migrate}", "*.rb")
|
|
|
|
Dir[migrations_root]
|
|
|
|
.map do |migration_filename|
|
|
|
|
File.basename(migration_filename)[/(^.*?)_/, 1].to_i
|
|
|
|
end
|
|
|
|
.sort
|
|
|
|
end
|
|
|
|
|
|
|
|
def cmd(operation, migration_number)
|
|
|
|
"rails db:migrate:#{operation} LOAD_PLUGINS=1 VERSION=#{migration_number}"
|
|
|
|
end
|
|
|
|
|
|
|
|
task :down, [:plugin] do |t, args|
|
|
|
|
list_migrations(args[:plugin]).reverse.each do |migration_number|
|
|
|
|
sh cmd(:down, migration_number)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
task :up, [:plugin] do |t, args|
|
|
|
|
list_migrations(args[:plugin]).each do |migration_number|
|
|
|
|
sh cmd(:up, migration_number)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-09-01 23:05:07 +08:00
|
|
|
|
|
|
|
desc 'display all plugin versions'
|
|
|
|
task 'plugin:versions' do |t, args|
|
|
|
|
versions =
|
|
|
|
Dir
|
|
|
|
.glob('*', base: 'plugins')
|
|
|
|
.map { |plugin|
|
|
|
|
[plugin, "plugins/#{plugin}", "plugins/#{plugin}/.git"]
|
|
|
|
}
|
|
|
|
.select { |plugin, plugin_dir, plugin_git_dir|
|
2022-04-07 00:29:39 +08:00
|
|
|
File.directory?(plugin_dir) && File.exist?(plugin_git_dir)
|
2020-09-01 23:05:07 +08:00
|
|
|
}
|
2022-04-07 00:29:39 +08:00
|
|
|
.sort_by { |plugin, _, _| plugin }
|
2020-09-01 23:05:07 +08:00
|
|
|
.map { |plugin, _, plugin_git_dir|
|
|
|
|
version = `git --git-dir \"#{plugin_git_dir}\" rev-parse HEAD`
|
|
|
|
abort("unable to get #{plugin} version") unless version
|
|
|
|
[plugin, version.strip[0...8]]
|
|
|
|
}
|
|
|
|
.to_h
|
|
|
|
|
|
|
|
puts JSON.pretty_generate(versions)
|
|
|
|
end
|