Skip to content

Commit

Permalink
wipwip
Browse files Browse the repository at this point in the history
  • Loading branch information
3v0k4 committed May 29, 2024
1 parent 6368cbe commit 974dfaf
Show file tree
Hide file tree
Showing 10 changed files with 153 additions and 97 deletions.
24 changes: 22 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,25 @@ jobs:
with:
ruby-version: ${{ matrix.ruby }}
bundler-cache: true
- run: bin/setup
- run: bin/rake test
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Build dockerfile.vips
uses: docker/build-push-action@v5
with:
context: .
file: ./Dockerfile.vips
tags: vips:latest
cache-from: type=gha
cache-to: type=gha,mode=max
load: true
- run: docker run --rm vips:latest bundle exec rake test X=/image_magick/
- name: Build dockerfile.imagemagick
uses: docker/build-push-action@v5
with:
context: .
file: ./Dockerfile.imagemagick
tags: imagemagick:latest
cache-from: type=gha
cache-to: type=gha,mode=max
load: true
- run: docker run --rm imagemagick:latest bundle exec rake test X=/vips/
19 changes: 19 additions & 0 deletions Dockerfile.imagemagick
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
FROM ruby:3.3

# throw errors if Gemfile has been modified since Gemfile.lock
RUN bundle config --global frozen 1

RUN apt-get update && apt-get install -y \
imagemagick \
inkscape \
&& rm -rf /var/lib/apt/lists/*

WORKDIR /usr/src/app

COPY lib/favicon_factory/version.rb ./lib/favicon_factory/version.rb
COPY favicon_factory.gemspec Gemfile Gemfile.lock .
RUN bundle install

COPY . .

CMD ["bin/console"]
20 changes: 20 additions & 0 deletions Dockerfile.vips
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
FROM ruby:3.3

# throw errors if Gemfile has been modified since Gemfile.lock
RUN bundle config --global frozen 1

RUN apt-get update && apt-get install -y \
libvips \
libvips-tools \
&& rm -rf /var/lib/apt/lists/*
RUN apt-get remove -y --purge imagemagick

WORKDIR /usr/src/app

COPY lib/favicon_factory/version.rb ./lib/favicon_factory/version.rb
COPY favicon_factory.gemspec Gemfile Gemfile.lock .
RUN bundle install

COPY . .

CMD ["bin/console"]
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ brew install vips
```

```bash
sudo apt install libvips
sudo apt-get install libvips
sudo apt-get install libvips-tools
```

ImageMagick and Inkscape:
Expand All @@ -48,8 +49,8 @@ brew install inkscape
```

```bash
sudo apt install imagemagick
sudo apt install inkscape
sudo apt-get install imagemagick
sudo apt-get install inkscape
```

Add `favicon_factory` to the Gemfile:
Expand Down
7 changes: 4 additions & 3 deletions bin/setup
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ FileUtils.chdir GEM_ROOT do
system! "brew install inkscape"
system! "brew install vips"
when "linux"
system! "sudo apt install imagemagick"
system! "sudo apt install inkscape"
system! "sudo apt install libvips"
system! "sudo apt-get install imagemagick"
system! "sudo apt-get install inkscape"
system! "sudo apt-get install libvips"
system! "sudo apt-get install libvips-tools"
else
raise "Unsupported platform: #{Gem::Platform.local.os}"
end
Expand Down
76 changes: 76 additions & 0 deletions build.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#1 [internal] load .dockerignore
#1 transferring context: 2B done
#1 DONE 0.0s

#2 [internal] load build definition from Dockerfile
#2 transferring dockerfile: 317B done
#2 DONE 0.0s

#3 [internal] load metadata for docker.io/library/ruby:3.3
#3 DONE 1.4s

#4 [1/7] FROM docker.io/library/ruby:3.3@sha256:797d68561a91415e05fd95178f467d86d77bce2d4f17f32683241a687cbd1705
#4 DONE 0.0s

#5 [internal] load build context
#5 transferring context: 50.74kB 0.1s done
#5 DONE 0.1s

#6 [2/7] RUN bundle config --global frozen 1
#6 CACHED

#7 [3/7] WORKDIR /usr/src/app
#7 CACHED

#8 [4/7] COPY lib/favicon_factory/version.rb favicon_factory.gemspec Gemfile Gemfile.lock ./
#8 CACHED

#9 [5/7] RUN ls
#9 CACHED

#10 [6/7] RUN bundle install
#10 0.363 Bundler 2.5.9 is running, but your lockfile was generated with 2.5.4. Installing Bundler 2.5.4 and restarting using that version.
#10 2.280 Fetching gem metadata from https://rubygems.org/.
#10 2.374 Fetching bundler 2.5.4
#10 2.716 Installing bundler 2.5.4
#10 3.217
#10 3.217 [!] There was an error parsing `Gemfile`:
#10 3.217 [!] There was an error while loading `favicon_factory.gemspec`: cannot load such file -- /usr/src/app/lib/favicon_factory/version. Bundler cannot continue.
#10 3.217
#10 3.217 # from /usr/src/app/favicon_factory.gemspec:3
#10 3.217 # -------------------------------------------
#10 3.217 #
#10 3.217 > require_relative "lib/favicon_factory/version"
#10 3.217 #
#10 3.217 # -------------------------------------------
#10 3.217 . Bundler cannot continue.
#10 3.217
#10 3.217 # from /usr/src/app/Gemfile:6
#10 3.217 # -------------------------------------------
#10 3.217 # # Specify your gem's dependencies in favicon_factory.gemspec
#10 3.217 > gemspec
#10 3.217 #
#10 3.217 # -------------------------------------------
#10 ERROR: process "/bin/sh -c bundle install" did not complete successfully: exit code: 14
------
> [6/7] RUN bundle install:
#10 3.217 #
#10 3.217 # -------------------------------------------
#10 3.217 . Bundler cannot continue.
#10 3.217
#10 3.217 # from /usr/src/app/Gemfile:6
#10 3.217 # -------------------------------------------
#10 3.217 # # Specify your gem's dependencies in favicon_factory.gemspec
#10 3.217 > gemspec
#10 3.217 #
#10 3.217 # -------------------------------------------
------
Dockerfile:10
--------------------
8 | COPY lib/favicon_factory/version.rb favicon_factory.gemspec Gemfile Gemfile.lock ./
9 | RUN ls
10 | >>> RUN bundle install
11 |
12 | COPY . .
--------------------
ERROR: failed to solve: process "/bin/sh -c bundle install" did not complete successfully: exit code: 14
23 changes: 5 additions & 18 deletions lib/favicon_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
autoload(:Vips, "vips")

module FaviconFactory
Params = Data.define(:favicon_svg, :background, :adapter) do
Params = Data.define(:favicon_svg, :background) do
def dir
File.dirname(favicon_svg)
end
Expand Down Expand Up @@ -50,16 +50,6 @@ class Command
desc "Background hex color for apple-touch-icon.png"
end

flag :vips do
long "--vips"
desc "Use Vips (default if installed)"
end

flag :"image-magick" do
long "--image-magick"
desc "Use ImageMagick"
end

flag :help do
short "-h"
long "--help"
Expand All @@ -82,7 +72,7 @@ def initialize(argv, stderr)
def call
params, status = parse(@argv)
return status if status >= 0
BaseAdapter.find(params.adapter).new(params, stderr).create_icons
BaseAdapter.find.new(params, stderr).create_icons
0
end

Expand All @@ -104,8 +94,7 @@ def parse(argv)
return exit_message(1, "Error: #{background} is not a valid color, use a hex value like #0099ff")
end

adapter = params[:vips] ? VipsAdapter : params[:"image-magick"] ? ImageMagickAdapter : nil
[Params.new(favicon_svg: favicon_svg, background: background, adapter: adapter), -1]
[Params.new(favicon_svg: favicon_svg, background: background), -1]
end

def hex?(string)
Expand All @@ -121,10 +110,8 @@ def exit_message(status, message)

class BaseAdapter
class << self
def find(adapter = nil)
return adapter if adapter
return VipsAdapter if TTY::Which.which("vips")
ImageMagickAdapter
def find
TTY::Which.which("vips") ? VipsAdapter : ImageMagickAdapter
end
end

Expand Down
30 changes: 1 addition & 29 deletions test/test_e2e.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
require "open3"

class TestE2e < Minitest::Test
def test_e2e__with_no_adapter__with_existing_svg__it_succeeds
def test_e2e__with_existing_svg__it_succeeds
with_svg do |dir, path|
_, stderr, status = Open3.capture3("bundle exec exe/favicon_factory #{path}")

Expand All @@ -17,32 +17,4 @@ def test_e2e__with_no_adapter__with_existing_svg__it_succeeds
end
end
end

def test_e2e__image_magick__with_existing_svg__it_succeeds
with_svg do |dir, path|
_, stderr, status = Open3.capture3("bundle exec exe/favicon_factory --image-magick #{path}")

assert_equal 0, status.exitstatus
assert_match Regexp.new("Info: Add the following to the `<head>`"), stderr
TARGETS.each do |name|
path = File.join(dir, name)
assert_match Regexp.new("Info: Generating #{path}"), stderr
assert_path_exists path
end
end
end

def test_e2e__vips__with_existing_svg__it_succeeds
with_svg do |dir, path|
_, stderr, status = Open3.capture3("bundle exec exe/favicon_factory --vips #{path}")

assert_equal 0, status.exitstatus
assert_match Regexp.new("Info: Add the following to the `<head>`"), stderr
TARGETS.each do |name|
path = File.join(dir, name)
assert_match Regexp.new("Info: Generating #{path}"), stderr
assert_path_exists path
end
end
end
end
21 changes: 1 addition & 20 deletions test/test_image_magick.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,12 @@ def setup
remove_const(:SVG_DENSITY)
const_set(:SVG_DENSITY, 1) # make tests faster
end

FaviconFactory::BaseAdapter.class_eval do
class << self
alias :find_ :find

def find(*)
FaviconFactory::ImageMagickAdapter
end
end
end
end

def teardown
FaviconFactory::BaseAdapter.class_eval do
class << self
remove_method(:find)
alias :find :find_
end
end
end

def test__image_magick__with_existing_files__it_skips
with_svg do |dir, path|
TARGETS.each { FileUtils.touch(File.join(dir, _1)) }
args = ["--image-magick", path]
args = [path]
stderr = StringIO.new
status = FaviconFactory::Cli.new(args, stderr).call

Expand Down
23 changes: 1 addition & 22 deletions test/test_vips.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,10 @@
require "stringio"

class TestVips < Minitest::Test
def setup
FaviconFactory::BaseAdapter.class_eval do
class << self
alias :find_ :find

def find(*)
FaviconFactory::VipsAdapter
end
end
end
end

def teardown
FaviconFactory::BaseAdapter.class_eval do
class << self
remove_method(:find)
alias :find :find_
end
end
end

def test__vips__with_existing_files__it_skips
with_svg do |dir, path|
TARGETS.each { FileUtils.touch(File.join(dir, _1)) }
args = ["--vips", path]
args = [path]
stderr = StringIO.new
status = FaviconFactory::Cli.new(args, stderr).call

Expand Down

0 comments on commit 974dfaf

Please sign in to comment.