From b0b1ef305b4e7db90291bcc84677bc84f23fb0f5 Mon Sep 17 00:00:00 2001 From: Piotr Sarnacki Date: Fri, 30 Jan 2015 15:29:44 +0100 Subject: [PATCH] Change the way config is stored On ember-cli config is stored in config/environment.js file and it can be accessed at any time of app being booted. Till now we were using Travis.config which was making things hard, because we needed an application instance to get any config value. This commit moves config to config/environment.js and allows to access it at any point of loading the app. --- assets/scripts/app/app.coffee | 3 +- assets/scripts/app/controllers/repos.coffee | 2 +- assets/scripts/app/initializers/config.coffee | 64 +---------------- assets/scripts/app/models/ssh-key.coffee | 2 +- assets/scripts/app/router.coffee | 6 +- assets/scripts/app/utils/auth.coffee | 6 +- .../utils/computed-limit.js} | 4 +- assets/scripts/app/utils/limited-array.coffee | 4 +- assets/scripts/app/utils/pusher.coffee | 12 ++-- assets/scripts/config/environment.js | 70 +++++++++++++++++++ assets/scripts/lib/travis/ajax.coffee | 6 +- assets/scripts/travis.coffee | 5 +- 12 files changed, 103 insertions(+), 81 deletions(-) rename assets/scripts/{lib/ext/ember/computed.js => app/utils/computed-limit.js} (91%) create mode 100644 assets/scripts/config/environment.js diff --git a/assets/scripts/app/app.coffee b/assets/scripts/app/app.coffee index 2db70e8c..6d92f52f 100644 --- a/assets/scripts/app/app.coffee +++ b/assets/scripts/app/app.coffee @@ -7,7 +7,8 @@ App = Ember.Application.extend(Ember.Evented, #LOG_RESOLVER: true setup: -> - @pusher = new Travis.Pusher(key: Travis.config.pusher_key, host: Travis.config.pusher_host) if Travis.config.pusher_key + if @config.pusher.key + @pusher = new Travis.Pusher(@config.pusher) @tailing = new Travis.Tailing($(window), '#tail', '#log') @toTop = new Travis.ToTop($(window), '.to-top', '#log-container') diff --git a/assets/scripts/app/controllers/repos.coffee b/assets/scripts/app/controllers/repos.coffee index 67d8c547..12ed798f 100644 --- a/assets/scripts/app/controllers/repos.coffee +++ b/assets/scripts/app/controllers/repos.coffee @@ -1,4 +1,4 @@ -require 'travis/limited-array' +require 'utils/limited-array' Repo = Travis.Repo limit = Ember.computed.limit diff --git a/assets/scripts/app/initializers/config.coffee b/assets/scripts/app/initializers/config.coffee index b2dbb2d0..ec85bec0 100644 --- a/assets/scripts/app/initializers/config.coffee +++ b/assets/scripts/app/initializers/config.coffee @@ -1,76 +1,16 @@ -loadConfig = -> - pages_endpoint = $('meta[rel="travis.pages_endpoint"]').attr('href') - billing_endpoint = $('meta[rel="travis.billing_endpoint"]').attr('href') - customer_io_site_id = $('meta[name="travis.customer_io_site_id"]').attr('value') - setupCustomerio(customer_io_site_id) if customer_io_site_id +require 'config/environment' - enterprise = $('meta[name="travis.enterprise"]').attr('value') == 'true' - - # for now I set pro to true also for enterprise, but it should be changed - # to allow more granular config later - pro = $('meta[name="travis.pro"]').attr('value') == 'true' || enterprise - - if config.pro - pusher = - channels: [] - channel_prefix: 'private-' - encrypted: true - key: '' - else - pusher = - channels: ['common'] - channel_prefix: '' - encrypted: false - - return { - syncingPageRedirectionTime: 5000 - api_endpoint: $('meta[rel="travis.api_endpoint"]').attr('href') - source_endpoint: $('meta[rel="travis.source_endpoint"]').attr('href') - pusher_key: $('meta[name="travis.pusher_key"]').attr('value') - pusher_host: $('meta[name="travis.pusher_host"]').attr('value') - pusher_path: $('meta[name="travis.pusher_path"]').attr('value') - ga_code: $('meta[name="travis.ga_code"]').attr('value') - code_climate: $('meta[name="travis.code_climate"]').attr('value') - ssh_key_enabled: $('meta[name="travis.ssh_key_enabled"]').attr('value') == 'true' - code_climate_url: $('meta[name="travis.code_climate_url"]').attr('value') - caches_enabled: $('meta[name="travis.caches_enabled"]').attr('value') == 'true' - show_repos_hint: 'private' - avatar_default_url: 'https://travis-ci.org/images/ui/default-avatar.png' - pusher_log_fallback: $('meta[name="travis.pusher_log_fallback"]').attr('value') == 'true' - pro: pro - enterprise: enterprise - sidebar_support_box: pro && !enterprise - - pages_endpoint: pages_endpoint || billing_endpoint - billing_endpoint: billing_endpoint - - url_legal: "#{billing_endpoint}/pages/legal" - url_imprint: "#{billing_endpoint}/pages/imprint" - url_security: "#{billing_endpoint}/pages/security" - url_terms: "#{billing_endpoint}/pages/terms" - customer_io_site_id: customer_io_site_id - - intervals: { times: -1, updateTimes: 1000 } - pusher: pusher - } +config = ENV.config initialize = (container, application) -> - config = application.config application.register 'config:main', config, { instantiate: false } application.inject('controller', 'config', 'config:main') application.inject('route', 'config', 'config:main') - application.inject('auth', 'config', 'config:main') - - application.pusher.config = config ConfigInitializer = name: 'config' initialize: initialize Ember.onLoad 'Ember.Application', (Application) -> - Application.config loadConfig() - - Application.ajax.pro = Application.config.pro - Application.initializer ConfigInitializer diff --git a/assets/scripts/app/models/ssh-key.coffee b/assets/scripts/app/models/ssh-key.coffee index a0e5795e..d288aef7 100644 --- a/assets/scripts/app/models/ssh-key.coffee +++ b/assets/scripts/app/models/ssh-key.coffee @@ -1,4 +1,4 @@ -Travis.Model = Model +Model = Travis.Model SshKey = Model.extend value: DS.attr() diff --git a/assets/scripts/app/router.coffee b/assets/scripts/app/router.coffee index fb35cc5d..45716538 100644 --- a/assets/scripts/app/router.coffee +++ b/assets/scripts/app/router.coffee @@ -1,6 +1,8 @@ require 'travis/location' require 'routes/application' +config = ENV.config + Router = Ember.Router.extend location: 'history' @@ -32,14 +34,14 @@ Router.map -> @resource 'pullRequests', path: '/pull_requests' @resource 'branches', path: '/branches' @resource 'requests', path: '/requests' - @resource 'caches', path: '/caches' + @resource 'caches', path: '/caches' if config.endpoints.caches @resource 'request', path: '/requests/:request_id' @resource 'settings', -> @route 'index', path: '/' @resource 'env_vars', -> @route 'new' - @resource 'ssh_key' + @resource 'ssh_key' if config.endpoints.ssh_key @route 'first_sync' @route 'insufficient_oauth_permissions' diff --git a/assets/scripts/app/utils/auth.coffee b/assets/scripts/app/utils/auth.coffee index 84666d1a..ce559923 100644 --- a/assets/scripts/app/utils/auth.coffee +++ b/assets/scripts/app/utils/auth.coffee @@ -1,3 +1,5 @@ +config = ENV.config + Auth = Ember.Object.extend state: "signed-out" receivingEnd: "#{location.protocol}//#{location.host}" @@ -9,7 +11,7 @@ Auth = Ember.Object.extend Travis.sessionStorage.getItem('travis.token') endpoint: (-> - @config.api_endpoint + config.api_endpoint ).property(), signOut: -> @@ -50,7 +52,7 @@ Auth = Ember.Object.extend validateUser: (user) -> fieldsToValidate = ['id', 'login', 'token', 'correct_scopes'] - if @config.pro + if config.pro fieldsToValidate.push 'channels' fieldsToValidate.every( (field) => @validateHas(field, user) ) && user.correct_scopes diff --git a/assets/scripts/lib/ext/ember/computed.js b/assets/scripts/app/utils/computed-limit.js similarity index 91% rename from assets/scripts/lib/ext/ember/computed.js rename to assets/scripts/app/utils/computed-limit.js index 089f8fe1..57787c03 100644 --- a/assets/scripts/lib/ext/ember/computed.js +++ b/assets/scripts/app/utils/computed-limit.js @@ -1,4 +1,4 @@ -Ember.computed.limit = function(dependentKey, limitKey) { +limit = function(dependentKey, limitKey) { var options = { addedItem: function(array, item, changeMeta, instanceMeta) { var limit = Ember.get(this, limitKey); @@ -24,3 +24,5 @@ Ember.computed.limit = function(dependentKey, limitKey) { }; return Ember.arrayComputed(dependentKey, limitKey, options); }; + +Ember.computed.limit = limit; diff --git a/assets/scripts/app/utils/limited-array.coffee b/assets/scripts/app/utils/limited-array.coffee index ad86d5ba..1228e1df 100644 --- a/assets/scripts/app/utils/limited-array.coffee +++ b/assets/scripts/app/utils/limited-array.coffee @@ -1,7 +1,9 @@ +computedLimit = Ember.computed.limit + LimitedArray = Ember.ArrayProxy.extend limit: 10 isLoadedBinding: 'content.isLoaded' - arrangedContent: Ember.computed.limit('content', 'limit') + arrangedContent: computedLimit('content', 'limit') totalLength: (-> @get('content.length') diff --git a/assets/scripts/app/utils/pusher.coffee b/assets/scripts/app/utils/pusher.coffee index 4fd55f64..adfadaa0 100644 --- a/assets/scripts/app/utils/pusher.coffee +++ b/assets/scripts/app/utils/pusher.coffee @@ -1,3 +1,5 @@ +config = ENV.config + TravisPusher = (config) -> @init(config) this @@ -7,7 +9,7 @@ TravisPusher.prototype.active_channels = [] TravisPusher.prototype.init = (config) -> Pusher.warn = @warn.bind(this) Pusher.host = config.host if config.host - @pusher = new Pusher(config.key, encrypted: @config.pusher.encrypted, disableStats: true) + @pusher = new Pusher(config.key, encrypted: config.encrypted, disableStats: true) @callbacksToProcess = [] @@ -97,9 +99,9 @@ TravisPusher.prototype.ignoreCode = (code) -> TravisPusher.prototype.ignoreMessage = (message) -> message.indexOf('Existing subscription') == 0 or message.indexOf('No current subscription') == 0 -Pusher.SockJSTransport.isSupported = -> false if pusher_host != 'ws.pusherapp.com' +Pusher.SockJSTransport.isSupported = -> false if config.pusher.host != 'ws.pusherapp.com' -if Travis.config.pro +if config.pro Pusher.channel_auth_transport = 'bulk_ajax' Pusher.authorizers.bulk_ajax = (socketId, _callback) -> @@ -122,8 +124,8 @@ if Travis.config.pro Pusher.getDefaultStrategy = (config) -> [ [":def", "ws_options", { - hostUnencrypted: config.wsHost + ":" + config.wsPort + (pusher_path && "/#{pusher_path}" || ''), - hostEncrypted: config.wsHost + ":" + config.wssPort + (pusher_path && "/#{pusher_path}" || '') + hostUnencrypted: config.wsHost + ":" + config.wsPort + (config.pusher.path && "/#{config.pusher.path}" || ''), + hostEncrypted: config.wsHost + ":" + config.wssPort + (config.pusher.path && "/#{config.pusher.path}" || '') path: config.path }], [":def", "sockjs_options", { diff --git a/assets/scripts/config/environment.js b/assets/scripts/config/environment.js new file mode 100644 index 00000000..6cfa403e --- /dev/null +++ b/assets/scripts/config/environment.js @@ -0,0 +1,70 @@ +var billing_endpoint, customer_io_site_id, enterprise, pages_endpoint, pro, pusher, pusher_host, pusher_key, pusher_log_fallback, pusher_path; + +pages_endpoint = $('meta[rel="travis.pages_endpoint"]').attr('href'); +billing_endpoint = $('meta[rel="travis.billing_endpoint"]').attr('href'); +customer_io_site_id = $('meta[name="travis.customer_io_site_id"]').attr('value'); + +if (customer_io_site_id) { + setupCustomerio(customer_io_site_id); +} + +enterprise = $('meta[name="travis.enterprise"]').attr('value') === 'true'; +pro = $('meta[name="travis.pro"]').attr('value') === 'true' || enterprise; +pusher_key = $('meta[name="travis.pusher_key"]').attr('value'); +pusher_host = $('meta[name="travis.pusher_host"]').attr('value'); +pusher_path = $('meta[name="travis.pusher_path"]').attr('value'); +pusher_log_fallback = $('meta[name="travis.pusher_log_fallback"]').attr('value') === 'true'; + +if (pro) { + pusher = { + channels: [], + channel_prefix: 'private-', + encrypted: true + }; +} else { + pusher = { + channels: ['common'], + channel_prefix: '', + encrypted: false + }; +} + +pusher.key = pusher_key; +pusher.host = pusher_host; +pusher.path = pusher_path; +pusher.log_fallback = pusher_log_fallback; + +config = { + syncingPageRedirectionTime: 5000, + api_endpoint: $('meta[rel="travis.api_endpoint"]').attr('href'), + source_endpoint: $('meta[rel="travis.source_endpoint"]').attr('href'), + ga_code: $('meta[name="travis.ga_code"]').attr('value'), + code_climate: $('meta[name="travis.code_climate"]').attr('value'), + endpoints: { + ssh_key: $('meta[name="travis.ssh_key_enabled"]').attr('value') === 'true', + caches: $('meta[name="travis.caches_enabled"]').attr('value') === 'true' + }, + code_climate_url: $('meta[name="travis.code_climate_url"]').attr('value'), + show_repos_hint: 'private', + avatar_default_url: 'https://travis-ci.org/images/ui/default-avatar.png', + pro: pro, + enterprise: enterprise, + sidebar_support_box: pro && !enterprise, + pages_endpoint: pages_endpoint || billing_endpoint, + billing_endpoint: billing_endpoint, + url_legal: billing_endpoint + "/pages/legal", + url_imprint: billing_endpoint + "/pages/imprint", + url_security: billing_endpoint + "/pages/security", + url_terms: billing_endpoint + "/pages/terms", + customer_io_site_id: customer_io_site_id, + intervals: { + times: -1, + updateTimes: 1000 + }, + pusher: pusher +}; + +if(!window.ENV) { + window.ENV = {}; +} +window.ENV.config = config; diff --git a/assets/scripts/lib/travis/ajax.coffee b/assets/scripts/lib/travis/ajax.coffee index 8b5e2d8f..ddd97c7b 100644 --- a/assets/scripts/lib/travis/ajax.coffee +++ b/assets/scripts/lib/travis/ajax.coffee @@ -1,3 +1,5 @@ +config = ENV.config + jQuery.support.cors = true default_options = @@ -18,7 +20,7 @@ Travis.ajax = Em.Object.create @ajax(url, 'patch', data: data, success: callback) needsAuth: (method, url) -> - return true if Travis.ajax.pro + return true if config.pro return true if method != 'GET' publicEndpoint = @publicEndpoints.find (pattern) -> @@ -36,7 +38,7 @@ Travis.ajax = Em.Object.create method = method || "GET" method = method.toUpperCase() - endpoint = Travis.config.api_endpoint || '' + endpoint = config.api_endpoint || '' options = options || {} token = Travis.sessionStorage.getItem('travis.token') diff --git a/assets/scripts/travis.coffee b/assets/scripts/travis.coffee index bf364cb0..3d826312 100644 --- a/assets/scripts/travis.coffee +++ b/assets/scripts/travis.coffee @@ -1,6 +1,5 @@ -require 'ext/jquery' -require 'ext/ember/namespace' -require 'ext/ember/computed' +require 'config/environment' +require 'utils/computed-limit' require 'app' window.ENV ||= {}