Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Browser working from git source #1187

Merged
merged 10 commits into from
Sep 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions .github/workflows/ruby_event_store-browser_assets.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
name: ruby_event_store-browser_assets
on:
push:

jobs:
test:
runs-on: ${{ matrix.os }}
env:
WORKING_DIRECTORY: ruby_event_store-browser
strategy:
fail-fast: false
matrix:
include:
- os: ubuntu-20.04
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: "16.10"
cache: "npm"
cache-dependency-path: ${{ env.WORKING_DIRECTORY }}/elm/package-lock.json
- run: make install-npm
working-directory: ${{ env.WORKING_DIRECTORY }}
- run: make build-npm
working-directory: ${{ env.WORKING_DIRECTORY }}
- uses: actions/upload-artifact@v2
with:
name: ruby_event_store_browser.js
path: ${{ env.WORKING_DIRECTORY }}/public/ruby_event_store_browser.js
- uses: actions/upload-artifact@v2
with:
name: ruby_event_store_browser.js.map
path: ${{ env.WORKING_DIRECTORY }}/public/ruby_event_store_browser.js.map
- uses: actions/upload-artifact@v2
with:
name: ruby_event_store_browser.css
path: ${{ env.WORKING_DIRECTORY }}/public/ruby_event_store_browser.css
- uses: actions/upload-artifact@v2
with:
name: ruby_event_store_browser.css.map
path: ${{ env.WORKING_DIRECTORY }}/public/ruby_event_store_browser.css.map
- uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: eu-central-1

- run: echo "::set-output name=short::$(git rev-parse --short=12 HEAD)"
id: sha
- run: |
aws s3 sync ${{ env.WORKING_DIRECTORY }}/public s3://ruby-event-store-assets/${{ steps.sha.outputs.short }}

2 changes: 2 additions & 0 deletions ruby_event_store-browser/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ dev: ## Start standalone browser seeded with some events and Elm/CSS recompile o
serve:
@bundle exec rackup --port 9393 devserver/config.ru

build-npm: $(JS_OUTFILE) $(CSS_OUTFILE)

install-npm:
@cd elm; npm install --no-fund --no-audit

Expand Down
1 change: 1 addition & 0 deletions ruby_event_store-browser/lib/ruby_event_store/browser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ module Browser
require_relative "browser/get_events_from_stream"
require_relative "browser/get_stream"
require_relative "browser/urls"
require_relative "browser/gem_source"
require_relative "browser/router"
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# frozen_string_literal: true

module RubyEventStore
module Browser
class GemSource
attr_reader :path

def initialize(load_path)
@path = load_path.find { |x| x.match? %r{ruby_event_store-browser(?:-\d\.\d\.\d)?/lib\z} }
end

def version
if from_rubygems?
path.split("/").fetch(-2).split("-").last
elsif from_git?
path.split("/").fetch(-3).split("-").last
end
end

def from_rubygems?
path.match? %r{/gems/ruby_event_store-browser-\d\.\d\.\d/lib\z}
end

def from_git?
path.match? %r{/bundler/gems/rails_event_store-[a-z0-9]{12}/ruby_event_store-browser/lib\z}
end
end
end
end
28 changes: 26 additions & 2 deletions ruby_event_store-browser/lib/ruby_event_store/browser/urls.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module RubyEventStore
module Browser
class Urls
Expand All @@ -20,6 +22,7 @@ def initialize(host, root_path, api_url)
@root_path = root_path
@app_url = [host, root_path].compact.reduce(:+)
@api_url = api_url || ("#{app_url}/api" if app_url)
@gem_source = GemSource.new($LOAD_PATH)
end

def events_url
Expand All @@ -45,11 +48,21 @@ def paginated_events_from_stream_url(id:, position: nil, direction: nil, count:
end

def browser_js_url
"#{app_url}/ruby_event_store_browser.js"
name = "ruby_event_store_browser.js"
if gem_source.from_git?
cdn_file_url(name)
else
local_file_url(name)
end
end

def browser_css_url
"#{app_url}/ruby_event_store_browser.css"
name = "ruby_event_store_browser.css"
if gem_source.from_git?
cdn_file_url(name)
else
local_file_url(name)
end
end

def bootstrap_js_url
Expand All @@ -59,6 +72,17 @@ def bootstrap_js_url
def ==(o)
self.class.eql?(o.class) && app_url.eql?(o.app_url) && api_url.eql?(o.api_url)
end

private
attr_reader :gem_source

def local_file_url(name)
"#{app_url}/#{name}"
end

def cdn_file_url(name)
"https://cdn.railseventstore.org/#{gem_source.version}/#{name}"
end
end
end
end
35 changes: 35 additions & 0 deletions ruby_event_store-browser/spec/gem_source_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
require "spec_helper"

module RubyEventStore
module Browser
RSpec.describe GemSource do
specify "git source" do
path = "/Users/mostlyobvious/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/bundler/gems/rails_event_store-151d0dfbec24/ruby_event_store-browser/lib"
source = GemSource.new([random_unrelated_path, path])

expect(source.version).to eq("151d0dfbec24")
expect(source).to be_from_git
end

specify "local path source" do
path = "/Users/mostlyobvious/Code/rails_event_store/ruby_event_store-browser/lib"
source = GemSource.new([random_unrelated_path, path])

expect(source.version).to be_nil
expect(source).not_to be_from_git
end

specify "rubygems source" do
path = "/Users/mostlyobvious/.rubies/ruby-3.1.2/lib/ruby/gems/3.1.0/gems/ruby_event_store-browser-2.5.1/lib"
source = GemSource.new([random_unrelated_path, path])

expect(source.version).to eq("2.5.1")
expect(source).not_to be_from_git
end

def random_unrelated_path
"/kaka/dudu"
end
end
end
end
14 changes: 14 additions & 0 deletions ruby_event_store-browser/spec/urls_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,20 @@ module Browser
routing = Urls.from_configuration("http://example.com:9393", "/res")
expect(routing.bootstrap_js_url).to eq("http://example.com:9393/res/bootstrap.js")
end

specify "browser_js_url when from git" do
git_source = double(:git_source, version: "deadbeef", from_git?: true)
allow(GemSource).to receive(:new).and_return(git_source)

expect(Urls.initial.browser_js_url).to eq("https://cdn.railseventstore.org/deadbeef/ruby_event_store_browser.js")
end

specify "browser_css_url when from git" do
git_source = double(:git_source, version: "deadbeef", from_git?: true)
allow(GemSource).to receive(:new).and_return(git_source)

expect(Urls.initial.browser_css_url).to eq("https://cdn.railseventstore.org/deadbeef/ruby_event_store_browser.css")
end
end
end
end