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

Commit

Permalink
Make shopify-extensions its own Ruby Gem extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
t6d committed Sep 10, 2021
1 parent 37fd292 commit c6c0d00
Show file tree
Hide file tree
Showing 7 changed files with 610 additions and 577 deletions.
14 changes: 0 additions & 14 deletions ext/shopify-cli/extconf.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
require "rbconfig"
require "fileutils"
require_relative "./install_extension_server.rb"

extension_server_version = "v0.1.0"
gem = File.expand_path("../../../", __FILE__)
exe = File.join(gem, "bin", "shopify")

Expand Down Expand Up @@ -35,12 +33,6 @@
\t echo "@ECHO OFF"> "#{bat}"
\t echo "#{script_content}">> "#{bat}"
MAKEFILE

ShopifyCli::InstallExtensionServer.call(
version: extension_server_version,
source: "shopify-extensions.exe",
target: File.join(File.dirname(__FILE__), "shopify-extensions.exe")
)
else
script = exe + ".sh"
symlink = "/usr/local/bin/shopify"
Expand All @@ -62,12 +54,6 @@
install: clean
\t@sudo ln -s #{script} #{symlink}
MAKEFILE

ShopifyCli::InstallExtensionServer.call(
version: extension_server_version,
source: "shopify-extensions",
target: File.join(File.dirname(__FILE__), "shopify-extensions")
)
end

File.write("Makefile", makefile_content)
15 changes: 15 additions & 0 deletions ext/shopify-extensions/extconf.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require_relative "./shopify_extensions"

File.write("Makefile", <<~MAKEFILE)
.PHONY: clean
clean: ;
install: ;
MAKEFILE

ShopifyExtensions.install(
version: "v0.1.0",
source: "shopify-extensions",
target: File.join(File.dirname(__FILE__), "shopify-extensions")
)
Original file line number Diff line number Diff line change
@@ -1,28 +1,46 @@
require 'open-uri'
require 'json'
require 'zlib'
require 'rubygems'
require 'rubygems/package'

module ShopifyCli
class InstallExtensionServer
require "rbconfig"
require "open-uri"
require "json"
require "zlib"
require "rubygems"
require "rubygems/package"

module ShopifyExtensions
class InstallationError < RuntimeError; end

def self.install(**args)
Install.call(**args)
end

class Install
def self.call(platform: Platform.new, **args)
new.call(platform: platform, **args)
end

def call(platform:, version:, source:, target:)
source = platform.format_path(source)
target = platform.format_path(target)

releases
.find { |release| release.version == version }
.download(platform: platform, source: source, target: target)

raise "Failed to install extension development server" unless File.executable?(target)
raise "Failed to isntall the correct extension development server version" unless `#{target} version`.strip == version.strip
raise not_executable_error unless File.executable?(target)
raise incorrect_version_error unless %x(#{target} version).strip == version.strip
end

private

def not_executable_error
InstallationError.new("Failed to install shopify-extensions")
end

def incorrect_version_error
InstallationError.new("Failed to install the correct version of shopify-extensions")
end

def releases
JSON.parse(URI.open(release_url).read).map(&Release)
JSON.parse(URI.parse(release_url).open.read).map(&Release)
end

def release_url
Expand Down Expand Up @@ -65,7 +83,7 @@ def self.to_proc
def download(source:, target:)
Dir.chdir(File.dirname(target)) do
File.open(File.basename(target), "w") do |target_file|
unpack(source, from: decompress(URI.open(url)), to: target_file)
unpack(source, from: decompress(URI.parse(url).open), to: target_file)
end
File.chmod(0755, target)
end
Expand Down Expand Up @@ -124,12 +142,21 @@ def initialize(ruby_config = RbConfig::CONFIG)
super(ruby_config)
end

def format_path(path)
case os
when "windows"
path + ".exe"
else
path
end
end

def to_s
format("%{os}-%{cpu}", os: os, cpu: cpu)
end

def os
case ruby_config.fetch('host_os')
case ruby_config.fetch("host_os")
when /linux/
"linux"
when /darwin/
Expand All @@ -140,7 +167,7 @@ def os
end

def cpu
case ruby_config.fetch('host_cpu')
case ruby_config.fetch("host_cpu")
when /arm.*64/
"arm64"
when /64/
Expand Down
2 changes: 1 addition & 1 deletion shopify-cli.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Gem::Specification.new do |spec|
end
spec.bindir = "bin"
spec.require_paths = ["lib", "vendor"]
spec.extensions = ["ext/shopify-cli/extconf.rb"]
spec.extensions = ["ext/shopify-cli/extconf.rb", "ext/shopify-extensions/extconf.rb"]
# Do NOT include `shopify` as a listed executable via `spec.executables`.
# `ext/shopify-cli/extconf.rb` will dynamically create a script and soft-link
# `/usr/local/bin/shopify` to that script, in order to "lock" the Ruby used to
Expand Down

0 comments on commit c6c0d00

Please sign in to comment.