From ae918976a30f8c9b561e216206dd309397c69017 Mon Sep 17 00:00:00 2001 From: Faizaan Gagan Date: Mon, 5 Apr 2021 21:53:34 +0530 Subject: [PATCH 1/8] DEV: add sprockets directive to require discourse folders --- assets/javascripts/wizard-custom.js | 71 +---------------------------- plugin.rb | 12 +++++ 2 files changed, 14 insertions(+), 69 deletions(-) diff --git a/assets/javascripts/wizard-custom.js b/assets/javascripts/wizard-custom.js index 5d18328f..8b30ad94 100644 --- a/assets/javascripts/wizard-custom.js +++ b/assets/javascripts/wizard-custom.js @@ -1,43 +1,4 @@ -//= require discourse/app/lib/autocomplete -//= require discourse/app/lib/utilities -//= require discourse/app/lib/offset-calculator -//= require discourse/app/lib/lock-on -//= require discourse/app/lib/text-direction -//= require discourse/app/lib/to-markdown -//= require discourse/app/lib/load-script -//= require discourse/app/lib/url -//= require discourse/app/lib/ajax -//= require discourse/app/lib/ajax-error -//= require discourse/app/lib/page-visible -//= require discourse/app/lib/logout -//= require discourse/app/lib/render-tag -//= require discourse/app/lib/notification-levels -//= require discourse/app/lib/computed -//= require discourse/app/lib/user-search -//= require discourse/app/lib/text -//= require discourse/app/lib/formatter -//= require discourse/app/lib/quote -//= require discourse/app/lib/link-mentions -//= require discourse/app/lib/link-hashtags -//= require discourse/app/lib/category-hashtags -//= require discourse/app/lib/tag-hashtags -//= require discourse/app/lib/uploads -//= require discourse/app/lib/category-tag-search -//= require discourse/app/lib/intercept-click -//= require discourse/app/lib/show-modal -//= require discourse/app/lib/key-value-store -//= require discourse/app/lib/settings -//= require discourse/app/lib/user-presence -//= require discourse/app/lib/hash -//= require discourse/app/lib/bookmark -//= require discourse/app/lib/put-cursor-at-end -//= require discourse/app/lib/safari-hacks -//= require discourse/app/lib/preload-store -//= require discourse/app/lib/topic-fancy-title -//= require discourse/app/lib/cookie -//= require discourse/app/lib/public-js-versions -//= require discourse/app/lib/load-oneboxes -//= require discourse/app/lib/highlight-syntax +//= require_tree_discourse discourse/app/lib //= require discourse/app/mixins/singleton //= require discourse/app/mixins/upload @@ -46,35 +7,7 @@ //= require message-bus -//= require discourse/app/models/login-method -//= require discourse/app/models/permission-type -//= require discourse/app/models/archetype -//= require discourse/app/models/rest -//= require discourse/app/models/site -//= require discourse/app/models/category -//= require discourse/app/models/session -//= require discourse/app/models/post-action-type -//= require discourse/app/models/trust-level -//= require discourse/app/models/store -//= require discourse/app/models/result-set -//= require discourse/app/models/bookmark -//= require discourse/app/models/user -//= require discourse/app/models/user-stream -//= require discourse/app/models/user-action -//= require discourse/app/models/user-action-group -//= require discourse/app/models/user-posts-stream -//= require discourse/app/models/badge -//= require discourse/app/models/badge-grouping -//= require discourse/app/models/user-badge -//= require discourse/app/models/topic -//= require discourse/app/models/action-summary -//= require discourse/app/models/user-action-stat -//= require discourse/app/models/user-drafts-stream -//= require discourse/app/models/user-draft -//= require discourse/app/models/composer -//= require discourse/app/models/draft -//= require discourse/app/models/group -//= require discourse/app/models/group-history +//= require_tree_discourse discourse/app/models //= require discourse/app/helpers/category-link //= require discourse/app/helpers/user-avatar diff --git a/plugin.rb b/plugin.rb index 0a272aed..48f0e0ef 100644 --- a/plugin.rb +++ b/plugin.rb @@ -97,6 +97,18 @@ after_initialize do load File.expand_path(path, __FILE__) end + add_to_class(::Sprockets::DirectiveProcessor, :process_require_tree_discourse_directive ) do |path = "."| + discourse_asset_path = "#{Rails.root}/app/assets/javascripts/" + path = File.expand_path(path, discourse_asset_path) + stat = @environment.stat(path) + + if stat && stat.directory? + require_paths(*@environment.stat_sorted_tree_with_dependencies(path)) + else + raise ArgumentError, "#{path} not found in discourse core" + end + end + add_class_method(:wizard, :user_requires_completion?) do |user| wizard_result = self.new(user).requires_completion? return wizard_result if wizard_result From 1bc55c609910361fbf2f43da0ef0675912a54b56 Mon Sep 17 00:00:00 2001 From: Faizaan Gagan Date: Mon, 5 Apr 2021 22:04:17 +0530 Subject: [PATCH 2/8] code formatting --- plugin.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin.rb b/plugin.rb index 48f0e0ef..a726c123 100644 --- a/plugin.rb +++ b/plugin.rb @@ -97,7 +97,7 @@ after_initialize do load File.expand_path(path, __FILE__) end - add_to_class(::Sprockets::DirectiveProcessor, :process_require_tree_discourse_directive ) do |path = "."| + add_to_class(::Sprockets::DirectiveProcessor, :process_require_tree_discourse_directive) do |path = "."| discourse_asset_path = "#{Rails.root}/app/assets/javascripts/" path = File.expand_path(path, discourse_asset_path) stat = @environment.stat(path) From 13eabe0695f650c4ea20fac05231434f2ec74392 Mon Sep 17 00:00:00 2001 From: Faizaan Gagan Date: Mon, 3 May 2021 03:42:26 +0530 Subject: [PATCH 3/8] added specs --- plugin.rb | 2 + spec/extensions/sprockets_directive_spec.rb | 55 +++++++++++++++++++ .../sprockets/require_tree_discourse_empty.js | 1 + .../require_tree_discourse_non_existant.js | 1 + .../sprockets/require_tree_discourse_test.js | 1 + .../sprockets/resolved_js_file_contents.txt | 3 + 6 files changed, 63 insertions(+) create mode 100644 spec/extensions/sprockets_directive_spec.rb create mode 100644 spec/fixtures/sprockets/require_tree_discourse_empty.js create mode 100644 spec/fixtures/sprockets/require_tree_discourse_non_existant.js create mode 100644 spec/fixtures/sprockets/require_tree_discourse_test.js create mode 100644 spec/fixtures/sprockets/resolved_js_file_contents.txt diff --git a/plugin.rb b/plugin.rb index eaa386b9..46c6fabb 100644 --- a/plugin.rb +++ b/plugin.rb @@ -97,6 +97,8 @@ after_initialize do end add_to_class(::Sprockets::DirectiveProcessor, :process_require_tree_discourse_directive) do |path = "."| + raise ArgumentError, "path cannot be empty" if path == "." + discourse_asset_path = "#{Rails.root}/app/assets/javascripts/" path = File.expand_path(path, discourse_asset_path) stat = @environment.stat(path) diff --git a/spec/extensions/sprockets_directive_spec.rb b/spec/extensions/sprockets_directive_spec.rb new file mode 100644 index 00000000..f5e2df9f --- /dev/null +++ b/spec/extensions/sprockets_directive_spec.rb @@ -0,0 +1,55 @@ +# frozen_string_literal: true + +require_relative '../plugin_helper' + +describe "Sprockets: require_tree_discourse directive" do + let(:discourse_asset_path) { + "#{Rails.root}/app/assets/javascripts/" + } + let(:fixture_asset_path) { + "#{Rails.root}/plugins/discourse-custom-wizard/spec/fixtures/sprockets/" + } + let(:test_file_contents) { + "console.log('hello')" + } + let(:resolved_file_contents) { + File.read( + "#{Rails.root}/plugins/discourse-custom-wizard/spec/fixtures/sprockets/resolved_js_file_contents.txt" + ) + } + + before do + @env ||= Sprockets::Environment.new + discourse_asset_path = "#{Rails.root}/app/assets/javascripts/" + fixture_asset_path = "#{Rails.root}/plugins/discourse-custom-wizard/spec/fixtures/sprockets/" + @env.append_path(discourse_asset_path) + @env.append_path(fixture_asset_path) + @env.cache = {} + end + + def create_tmp_folder_and_run(path, file_contents, &block) + dir = File.dirname(path) + unless File.directory?(dir) + FileUtils.mkdir_p(dir) + end + + File.new(path, 'w') + File.write(path, file_contents) + yield block if block_given? + File.delete(path) + end + + it "includes assets from the discourse core" do + create_tmp_folder_and_run("#{discourse_asset_path}/sptest/test.js", test_file_contents) do + expect(@env.find_asset("require_tree_discourse_test.js").to_s).to eq(resolved_file_contents) + end + end + + it "throws ArgumentError if path is empty" do + expect { @env.find_asset("require_tree_discourse_empty.js") }.to raise_error(ArgumentError).with_message("path cannot be empty") + end + + it "throws ArgumentError if path is non non-existent" do + expect { @env.find_asset("require_tree_discourse_non_existant.js") }.to raise_error(ArgumentError) + end +end diff --git a/spec/fixtures/sprockets/require_tree_discourse_empty.js b/spec/fixtures/sprockets/require_tree_discourse_empty.js new file mode 100644 index 00000000..df264ec5 --- /dev/null +++ b/spec/fixtures/sprockets/require_tree_discourse_empty.js @@ -0,0 +1 @@ +//= require_tree_discourse \ No newline at end of file diff --git a/spec/fixtures/sprockets/require_tree_discourse_non_existant.js b/spec/fixtures/sprockets/require_tree_discourse_non_existant.js new file mode 100644 index 00000000..d9b2be76 --- /dev/null +++ b/spec/fixtures/sprockets/require_tree_discourse_non_existant.js @@ -0,0 +1 @@ +//= require_tree_discourse dummy_path \ No newline at end of file diff --git a/spec/fixtures/sprockets/require_tree_discourse_test.js b/spec/fixtures/sprockets/require_tree_discourse_test.js new file mode 100644 index 00000000..a86aa0d7 --- /dev/null +++ b/spec/fixtures/sprockets/require_tree_discourse_test.js @@ -0,0 +1 @@ +//= require_tree_discourse sptest \ No newline at end of file diff --git a/spec/fixtures/sprockets/resolved_js_file_contents.txt b/spec/fixtures/sprockets/resolved_js_file_contents.txt new file mode 100644 index 00000000..53e2cfa2 --- /dev/null +++ b/spec/fixtures/sprockets/resolved_js_file_contents.txt @@ -0,0 +1,3 @@ +eval("define(\"sptest/test\", [], function () {\n \"use strict\";\n\n console.log('hello');\n});" + "\n//# sourceURL=sptest/test"); +; +eval("" + "\n//# sourceURL=require_tree_discourse_test"); From 77dd879f802571b226dc3e19c6eee283ab8804aa Mon Sep 17 00:00:00 2001 From: Faizaan Gagan Date: Mon, 3 May 2021 03:43:47 +0530 Subject: [PATCH 4/8] minor formatting --- spec/extensions/sprockets_directive_spec.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/spec/extensions/sprockets_directive_spec.rb b/spec/extensions/sprockets_directive_spec.rb index f5e2df9f..c5eec7a0 100644 --- a/spec/extensions/sprockets_directive_spec.rb +++ b/spec/extensions/sprockets_directive_spec.rb @@ -4,7 +4,7 @@ require_relative '../plugin_helper' describe "Sprockets: require_tree_discourse directive" do let(:discourse_asset_path) { - "#{Rails.root}/app/assets/javascripts/" + "#{Rails.root}/app/assets/javascripts/" } let(:fixture_asset_path) { "#{Rails.root}/plugins/discourse-custom-wizard/spec/fixtures/sprockets/" @@ -13,9 +13,9 @@ describe "Sprockets: require_tree_discourse directive" do "console.log('hello')" } let(:resolved_file_contents) { - File.read( - "#{Rails.root}/plugins/discourse-custom-wizard/spec/fixtures/sprockets/resolved_js_file_contents.txt" - ) + File.read( + "#{Rails.root}/plugins/discourse-custom-wizard/spec/fixtures/sprockets/resolved_js_file_contents.txt" + ) } before do From cdb9ec6c3eafa25bb36d8f1a0a70bf97edc14420 Mon Sep 17 00:00:00 2001 From: Faizaan Gagan Date: Mon, 3 May 2021 04:19:27 +0530 Subject: [PATCH 5/8] remove the directory after spec run --- spec/extensions/sprockets_directive_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/extensions/sprockets_directive_spec.rb b/spec/extensions/sprockets_directive_spec.rb index c5eec7a0..d268fd1d 100644 --- a/spec/extensions/sprockets_directive_spec.rb +++ b/spec/extensions/sprockets_directive_spec.rb @@ -36,7 +36,7 @@ describe "Sprockets: require_tree_discourse directive" do File.new(path, 'w') File.write(path, file_contents) yield block if block_given? - File.delete(path) + FileUtils.rm_r(dir) end it "includes assets from the discourse core" do From 5f24882ef6ecc188683ce0b8957d543d015ea131 Mon Sep 17 00:00:00 2001 From: Faizaan Gagan Date: Sat, 8 May 2021 23:15:06 +0530 Subject: [PATCH 6/8] DEV: raise plugin specific errors on failure --- plugin.rb | 5 +++-- spec/extensions/sprockets_directive_spec.rb | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/plugin.rb b/plugin.rb index 46c6fabb..1f917493 100644 --- a/plugin.rb +++ b/plugin.rb @@ -74,6 +74,7 @@ after_initialize do ../lib/custom_wizard/api/endpoint.rb ../lib/custom_wizard/api/log_entry.rb ../lib/custom_wizard/liquid_extensions/first_non_empty.rb + ../lib/custom_wizard/exceptions/exceptions.rb ../serializers/custom_wizard/api/authorization_serializer.rb ../serializers/custom_wizard/api/basic_endpoint_serializer.rb ../serializers/custom_wizard/api/endpoint_serializer.rb @@ -97,7 +98,7 @@ after_initialize do end add_to_class(::Sprockets::DirectiveProcessor, :process_require_tree_discourse_directive) do |path = "."| - raise ArgumentError, "path cannot be empty" if path == "." + raise CustomWizard::SprocketsEmptyPath, "path cannot be empty" if path == "." discourse_asset_path = "#{Rails.root}/app/assets/javascripts/" path = File.expand_path(path, discourse_asset_path) @@ -106,7 +107,7 @@ after_initialize do if stat && stat.directory? require_paths(*@environment.stat_sorted_tree_with_dependencies(path)) else - raise ArgumentError, "#{path} not found in discourse core" + raise CustomWizard::SprocketsFileNotFound, "#{path} not found in discourse core" end end diff --git a/spec/extensions/sprockets_directive_spec.rb b/spec/extensions/sprockets_directive_spec.rb index d268fd1d..5a074040 100644 --- a/spec/extensions/sprockets_directive_spec.rb +++ b/spec/extensions/sprockets_directive_spec.rb @@ -46,10 +46,10 @@ describe "Sprockets: require_tree_discourse directive" do end it "throws ArgumentError if path is empty" do - expect { @env.find_asset("require_tree_discourse_empty.js") }.to raise_error(ArgumentError).with_message("path cannot be empty") + expect { @env.find_asset("require_tree_discourse_empty.js") }.to raise_error(CustomWizard::SprocketsEmptyPath).with_message("path cannot be empty") end it "throws ArgumentError if path is non non-existent" do - expect { @env.find_asset("require_tree_discourse_non_existant.js") }.to raise_error(ArgumentError) + expect { @env.find_asset("require_tree_discourse_non_existant.js") }.to raise_error(CustomWizard::SprocketsFileNotFound) end end From 3a3d23a317206e9adc56d6b864d50b3f22a53fb7 Mon Sep 17 00:00:00 2001 From: Faizaan Gagan Date: Sat, 8 May 2021 23:18:58 +0530 Subject: [PATCH 7/8] added missing file --- lib/custom_wizard/exceptions/exceptions.rb | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 lib/custom_wizard/exceptions/exceptions.rb diff --git a/lib/custom_wizard/exceptions/exceptions.rb b/lib/custom_wizard/exceptions/exceptions.rb new file mode 100644 index 00000000..5560efc8 --- /dev/null +++ b/lib/custom_wizard/exceptions/exceptions.rb @@ -0,0 +1,4 @@ +module CustomWizard + class SprocketsFileNotFound < StandardError; end + class SprocketsEmptyPath < StandardError; end +end \ No newline at end of file From 4cacd849677b8fe41d4281c2724bc0e2a3e90207 Mon Sep 17 00:00:00 2001 From: Faizaan Gagan Date: Sat, 8 May 2021 23:38:26 +0530 Subject: [PATCH 8/8] code formatting --- lib/custom_wizard/exceptions/exceptions.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/custom_wizard/exceptions/exceptions.rb b/lib/custom_wizard/exceptions/exceptions.rb index 5560efc8..b5014d27 100644 --- a/lib/custom_wizard/exceptions/exceptions.rb +++ b/lib/custom_wizard/exceptions/exceptions.rb @@ -1,4 +1,5 @@ +# frozen_string_literal: true module CustomWizard class SprocketsFileNotFound < StandardError; end class SprocketsEmptyPath < StandardError; end -end \ No newline at end of file +end