Skip to content
This repository has been archived by the owner on Jun 1, 2023. It is now read-only.

Commit

Permalink
send script configuration ui definition during push (#1095)
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobsteves authored Mar 9, 2021
1 parent 67d763d commit b30efb5
Show file tree
Hide file tree
Showing 19 changed files with 201 additions and 66 deletions.
2 changes: 1 addition & 1 deletion lib/project_types/script/commands/push.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def call(_args, _name)
Layers::Application::PushScript.call(ctx: @ctx, force: options.flags.key?(:force))
@ctx.puts(@ctx.message("script.push.script_pushed", api_key: api_key))
rescue StandardError => e
msg = @ctx.message("script.push.error.operation_failed", api_key: ScriptProject.current.api_key)
msg = @ctx.message("script.push.error.operation_failed", api_key: ShopifyCli::Project.current.env.api_key)
UI::ErrorHandler.pretty_print_and_raise(e, failed_op: msg)
end

Expand Down
17 changes: 17 additions & 0 deletions lib/project_types/script/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,23 @@ module Script
module Errors
class InvalidContextError < ScriptProjectError; end
class InvalidScriptNameError < ScriptProjectError; end

class InvalidConfigUiDefinitionError < ScriptProjectError
attr_reader :filename
def initialize(filename)
super()
@filename = filename
end
end

class MissingSpecifiedConfigUiDefinitionError < ScriptProjectError
attr_reader :filename
def initialize(filename)
super()
@filename = filename
end
end

class NoExistingAppsError < ScriptProjectError; end
class NoExistingOrganizationsError < ScriptProjectError; end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ mutation AppScriptUpdateOrCreate(
$extensionPointName: ExtensionPointName!,
$title: String,
$description: String,
$configUi: String,
$sourceCode: String,
$language: String,
$force: Boolean,
Expand All @@ -13,6 +14,7 @@ mutation AppScriptUpdateOrCreate(
extensionPointName: $extensionPointName
title: $title
description: $description
configUi: $configUi
sourceCode: $sourceCode
language: $language
force: $force
Expand Down
8 changes: 3 additions & 5 deletions lib/project_types/script/layers/application/build_script.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,15 @@ module Layers
module Application
class BuildScript
class << self
def call(ctx:, task_runner:, script_name:, description:, extension_point_type:)
def call(ctx:, task_runner:, script_project:)
CLI::UI::Frame.open(ctx.message("script.application.building")) do
begin
UI::StrictSpinner.spin(ctx.message("script.application.building_script")) do |spinner|
Infrastructure::PushPackageRepository.new(ctx: ctx).create_push_package(
extension_point_type: extension_point_type,
script_name: script_name,
description: description,
script_project: script_project,
script_content: task_runner.build,
compiled_type: task_runner.compiled_type,
metadata: task_runner.metadata
metadata: task_runner.metadata,
)
spinner.update_title(ctx.message("script.application.built"))
end
Expand Down
12 changes: 2 additions & 10 deletions lib/project_types/script/layers/application/push_script.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,11 @@ def call(ctx:, force:)
script_project = ScriptProject.current
task_runner = Infrastructure::TaskRunner.for(ctx, script_project.language, script_project.script_name)
ProjectDependencies.install(ctx: ctx, task_runner: task_runner)
BuildScript.call(
ctx: ctx,
task_runner: task_runner,
extension_point_type: script_project.extension_point_type,
script_name: script_project.script_name,
description: script_project.description
)
BuildScript.call(ctx: ctx, task_runner: task_runner, script_project: script_project)

UI::PrintingSpinner.spin(ctx, ctx.message("script.application.pushing")) do |p_ctx, spinner|
package = Infrastructure::PushPackageRepository.new(ctx: p_ctx).get_push_package(
extension_point_type: script_project.extension_point_type,
script_name: script_project.script_name,
description: script_project.description,
script_project: script_project,
compiled_type: task_runner.compiled_type,
metadata: task_runner.metadata
)
Expand Down
6 changes: 5 additions & 1 deletion lib/project_types/script/layers/domain/push_package.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class PushPackage
:extension_point_type,
:script_name,
:description,
:config_ui,
:script_content,
:compiled_type,
:metadata
Expand All @@ -19,7 +20,8 @@ def initialize(
description:,
script_content:,
compiled_type:,
metadata:
metadata:,
config_ui:
)
@id = id
@extension_point_type = extension_point_type
Expand All @@ -28,6 +30,7 @@ def initialize(
@script_content = script_content
@compiled_type = compiled_type
@metadata = metadata
@config_ui = config_ui
end

def push(script_service, api_key, force)
Expand All @@ -40,6 +43,7 @@ def push(script_service, api_key, force)
api_key: api_key,
force: force,
metadata: @metadata,
config_ui: @config_ui,
)
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,42 +7,37 @@ class PushPackageRepository
include SmartProperties
property! :ctx, accepts: ShopifyCli::Context

def create_push_package(
extension_point_type:,
script_name:,
description:,
script_content:,
compiled_type:,
metadata:
)
build_file_path = file_path(script_name, compiled_type)
def create_push_package(script_project:, script_content:, compiled_type:, metadata:)
build_file_path = file_path(script_project.script_name, compiled_type)
write_to_path(build_file_path, script_content)

Domain::PushPackage.new(
id: build_file_path,
extension_point_type: extension_point_type,
script_name: script_name,
description: description,
extension_point_type: script_project.extension_point_type,
script_name: script_project.script_name,
description: script_project.description,
script_content: script_content,
compiled_type: compiled_type,
metadata: metadata,
config_ui: script_project.config_ui,
)
end

def get_push_package(extension_point_type:, script_name:, description:, compiled_type:, metadata:)
build_file_path = file_path(script_name, compiled_type)
def get_push_package(script_project:, compiled_type:, metadata:)
build_file_path = file_path(script_project.script_name, compiled_type)
raise Domain::PushPackageNotFoundError unless ctx.file_exist?(build_file_path)

script_content = File.read(build_file_path)

Domain::PushPackage.new(
id: build_file_path,
extension_point_type: extension_point_type,
script_name: script_name,
description: description,
extension_point_type: script_project.extension_point_type,
script_name: script_project.script_name,
description: script_project.description,
script_content: script_content,
compiled_type: compiled_type,
metadata: metadata,
config_ui: script_project.config_ui,
)
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ def push(
description: nil,
api_key: nil,
force: false,
metadata:
metadata:,
config_ui:
)
query_name = "app_script_update_or_create"
variables = {
extensionPointName: extension_point_type.upcase,
title: script_name,
description: description,
configUi: config_ui,
sourceCode: Base64.encode64(script_content),
language: compiled_type,
force: force,
Expand Down
6 changes: 6 additions & 0 deletions lib/project_types/script/messages/messages.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ module Messages
invalid_config: "Can't change the configuration values because %1$s is missing or "\
"it is not formatted properly.",

invalid_config_ui_definition_cause: "The UI configuration file %s contains invalid YAML.",
invalid_config_ui_definition_help: "Fix the errors and try again.",

missing_config_ui_definition_cause: "You are missing the UI configuration file %s.",
missing_config_ui_definition_help: "Create this file and try again.",

script_not_found_cause: "Couldn't find script %s for extension point %s",

service_failure_cause: "Internal service error.",
Expand Down
24 changes: 23 additions & 1 deletion lib/project_types/script/script_project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

module Script
class ScriptProject < ShopifyCli::Project
attr_reader :extension_point_type, :script_name, :language, :description
attr_reader :extension_point_type, :script_name, :language, :description, :config_ui

def initialize(*args)
super
@extension_point_type = lookup_config!("extension_point_type")
raise Errors::DeprecatedEPError, @extension_point_type if deprecated?(@extension_point_type)
@script_name = lookup_config!("script_name")
@description = lookup_config("description")
@config_ui = lookup_config_ui
@language = lookup_language
ShopifyCli::Core::Monorail.metadata = {
"script_name" => @script_name,
Expand Down Expand Up @@ -38,6 +39,19 @@ def lookup_config!(key)
config[key]
end

def lookup_config_ui
filename = lookup_config("config_ui_file")
return nil unless filename

path = File.join(directory, filename)
raise Errors::MissingSpecifiedConfigUiDefinitionError, filename unless File.exist?(path)

contents = File.read(path)
raise Errors::InvalidConfigUiDefinitionError, filename unless valid_config_ui?(contents)

contents
end

def lookup_language
lang = lookup_config("language")&.downcase || Layers::Domain::ExtensionPointAssemblyScriptSDK.language
if Layers::Application::ExtensionPoints.supported_language?(type: extension_point_type, language: lang)
Expand All @@ -47,6 +61,14 @@ def lookup_language
end
end

def valid_config_ui?(raw_yaml)
require "yaml" # takes 20ms, so deferred as late as possible.
YAML.safe_load(raw_yaml)
true
rescue Psych::SyntaxError
false
end

class << self
def create(ctx, dir)
raise Errors::ScriptProjectAlreadyExistsError, dir if ctx.dir_exist?(dir)
Expand Down
12 changes: 12 additions & 0 deletions lib/project_types/script/ui/error_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,18 @@ def self.error_messages(e)
Script::Layers::Application::ExtensionPoints.languages(type: e.extension_point_type).join(", ")
),
}
when Errors::InvalidConfigUiDefinitionError
{
cause_of_error: ShopifyCli::Context
.message("script.error.invalid_config_ui_definition_cause", e.filename),
help_suggestion: ShopifyCli::Context.message("script.error.invalid_config_ui_definition_help"),
}
when Errors::MissingSpecifiedConfigUiDefinitionError
{
cause_of_error: ShopifyCli::Context
.message("script.error.missing_config_ui_definition_cause", e.filename),
help_suggestion: ShopifyCli::Context.message("script.error.missing_config_ui_definition_help"),
}
when Errors::InvalidScriptNameError
{
cause_of_error: ShopifyCli::Context.message("script.error.invalid_script_name_cause"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,13 @@
let(:compiled_type) { "wasm" }
let(:metadata) { Script::Layers::Domain::Metadata.new("1", "0", false) }
let(:task_runner) { stub(compiled_type: compiled_type, metadata: metadata) }
let(:script_project) { stub }

subject do
Script::Layers::Application::BuildScript.call(
ctx: @context,
task_runner: task_runner,
script_name: script_name,
extension_point_type: extension_point_type,
description: description
script_project: script_project
)
end

Expand All @@ -30,9 +29,7 @@
CLI::UI::Frame.expects(:with_frame_color_override).never
task_runner.expects(:build).returns(content)
Script::Layers::Infrastructure::PushPackageRepository.any_instance.expects(:create_push_package).with(
extension_point_type: extension_point_type,
script_name: script_name,
description: description,
script_project: script_project,
script_content: content,
compiled_type: "wasm",
metadata: metadata
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
extension_point_type: extension_point_type,
script_name: script_name,
description: description,
config_ui: "",
env: { api_key: api_key }
)
end
Expand All @@ -41,9 +42,7 @@
Script::ScriptProject.stubs(:current).returns(project)
extension_point_repository.create_extension_point(extension_point_type)
push_package_repository.create_push_package(
extension_point_type: extension_point_type,
script_name: script_name,
description: description,
script_project: project,
script_content: "content",
compiled_type: compiled_type,
metadata: metadata
Expand All @@ -60,9 +59,7 @@
Script::Layers::Application::BuildScript.expects(:call).with(
ctx: @context,
task_runner: task_runner,
extension_point_type: extension_point_type,
script_name: script_name,
description: description
script_project: project
)
Script::Layers::Infrastructure::ScriptService
.expects(:new).returns(script_service_instance)
Expand Down
2 changes: 2 additions & 0 deletions test/project_types/script/layers/domain/push_package_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
let(:script_id) { "id" }
let(:script_name) { "foo_script" }
let(:description) { "my description" }
let(:config_ui) { "---\nversion: 1\n" }
let(:api_key) { "fake_key" }
let(:force) { false }
let(:script_content) { "(module)" }
Expand All @@ -18,6 +19,7 @@
extension_point_type: extension_point_type,
script_name: script_name,
description: description,
config_ui: config_ui,
script_content: script_content,
compiled_type: compiled_type,
metadata: metadata
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,27 @@ def initialize
end

def create_push_package(
extension_point_type:,
script_name:,
description:,
script_project:,
script_content:,
compiled_type:,
metadata:
)
id = id(script_name, compiled_type)
id = id(script_project.script_name, compiled_type)
@cache[id] = Domain::PushPackage.new(
id: id,
extension_point_type: extension_point_type,
script_name: script_name,
description: description,
extension_point_type: script_project.extension_point_type,
script_name: script_project.script_name,
description: script_project.description,
script_content: script_content,
compiled_type: compiled_type,
metadata: metadata
metadata: metadata,
config_ui: script_project.config_ui,
)
end

def get_push_package(extension_point_type:, script_name:, description:, compiled_type:, metadata:)
_ = extension_point_type, description, metadata
id = id(script_name, compiled_type)
def get_push_package(script_project:, compiled_type:, metadata:)
_ = metadata
id = id(script_project.script_name, compiled_type)
if @cache.key?(id)
@cache[id]
else
Expand Down
Loading

0 comments on commit b30efb5

Please sign in to comment.