Skip to content

mfinelli/bankrupt

Repository files navigation

Bankrupt

RubyGems Build Status Coverage Status Maintainability Inline docs

A sinatra helper to path assets during development and production.

Usage

Sinatra

Before loading your app set a few constants:

CDN = CONFIG[:cdn_url].to_s.freeze

require 'bankrupt/util'
ASSETS = Bankrupt::Util.parse_manifest(
  File.join(APP_ROOT, 'tmp', 'assets.json')
).freeze

Then include bankrupt as a helper in your app:

require 'bankrupt'

class App < Sinatra::Base
  set :public_folder, File.join(APP_ROOT, 'public')

  helpers Bankrupt

  # TODO: there is a better way to do this
  def initialize
    @_assets = {}
    super
  end
end

Rake

You can use the bundled rake task to generate the manifest file in the correct format using any assets found in the public folder. You can also upload the assets to an s3 bucket for use with cloudfront as a CDN.

Make sure to add the extra dependencies to your gemfile:

gem 'aws-sdk-s3'
gem 'mini_mime'

In your rakefile you'll need to define several constants and then include the tasks:

require 'bankrupt/tasks'
require 'logger'

APP_ROOT = __dir__.freeze unless defined?(APP_ROOT)
CDN_BUCKET = 'your-s3-bucket'.freeze unless defined?(CDN_BUCKET)
CDN_PREFIX = 'project'.freeze unless defined?(CDN_PREFIX)

unless defined?(VERSION)
  VERSION = JSON.parse(File.read(File.join(APP_ROOT, 'package.json')),
                       symbolize_names: true).fetch(:version).freeze
end

LOG = Logger.new(STDOUT) unless defined?(LOG)

Finally set your default task:

task default: if ENV['CLOUDBUILD'].to_s.casecmp?('true')
                %i[bankrupt:cdn]
              else
                %i[bankrupt:manifest]
              end

License

Copyright 2018 Mario Finelli

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.