Refactor Travis.Helpers to prepare it for ES6 modules

In order to make it easy to switch to ember-cli I'm starting changing a series
of refactorings that will prepare the codebase to the switch. Here I'm:

1. Assign all of the external objects and functions to variables in order to
   make it easy to change them to imports in the feature
2. Extract functions from Travis.Helpers into local functions and create the
   object at the end of file to prepare it for exporting
This commit is contained in:
Piotr Sarnacki 2015-01-14 12:55:17 +01:00
parent 2b86e93d13
commit ec8d93f8cf

View File

@ -1,40 +1,49 @@
require 'config/emoij' require 'config/emoij'
@Travis.Helpers = config_keys_map = Travis.CONFIG_KEYS_MAP
COLORS: config = Travis.config
githubCommitUrl = Travis.Urls.githubCommit
currentDate = Travis.currentDate
timeago = $.timeago
intersect = $.intersect
only = $.only
mapObject = $.map
colors = {
default: 'yellow' default: 'yellow'
passed: 'green' passed: 'green'
failed: 'red' failed: 'red'
errored: 'gray' errored: 'gray'
canceled: 'gray' canceled: 'gray'
}
compact: (object) -> compact = (object) ->
result = {} result = {}
(result[key] = value unless $.isEmpty(value)) for key, value of object || {} (result[key] = value unless Ember.isEmpty(value)) for key, value of object || {}
result result
safe: (string) -> safe = (string) ->
new Handlebars.SafeString(string) new Ember.Handlebars.SafeString(string)
colorForState: (state) -> colorForState = (state) ->
Travis.Helpers.COLORS[state] || Travis.Helpers.COLORS['default'] colors[state] || colors['default']
formatCommit: (sha, branch) -> formatCommit = (sha, branch) ->
Travis.Helpers.formatSha(sha) + if branch then " (#{branch})" else '' formatSha(sha) + if branch then " (#{branch})" else ''
formatSha: (sha) -> formatSha = (sha) ->
(sha || '').substr(0, 7) (sha || '').substr(0, 7)
formatConfig: (config) -> formatConfig = (config) ->
config = $.only config, Object.keys(Travis.CONFIG_KEYS_MAP) config = only config, Object.keys(config_keys_map)
values = $.map config, (value, key) -> values = mapObject config, (value, key) ->
value = (if value && value.join then value.join(', ') else value) || '' value = (if value && value.join then value.join(', ') else value) || ''
if key == 'rvm' && "#{value}".match(/^\d+$/) if key == 'rvm' && "#{value}".match(/^\d+$/)
value = "#{value}.0" value = "#{value}.0"
'%@: %@'.fmt Travis.CONFIG_KEYS_MAP[key], value '%@: %@'.fmt config_keys_map[key], value
if values.length == 0 then '-' else values.join(', ') if values.length == 0 then '-' else values.join(', ')
formatMessage: (message, options) -> formatMessage = (message, options) ->
message = message || '' message = message || ''
message = message.split(/\n/)[0] if options.short message = message.split(/\n/)[0] if options.short
message = @_emojize(@_escape(message)) message = @_emojize(@_escape(message))
@ -44,18 +53,18 @@ require 'config/emoij'
message = message.replace /\n/g, '<br/>' message = message.replace /\n/g, '<br/>'
message message
pathFrom: (url) -> pathFrom = (url) ->
(url || '').split('/').pop() (url || '').split('/').pop()
timeAgoInWords: (date) -> timeAgoInWords = (date) ->
$.timeago.distanceInWords date timeago.distanceInWords date
durationFrom: (started, finished) -> durationFrom = (started, finished) ->
started = started and @_toUtc(new Date(@_normalizeDateString(started))) started = started and @_toUtc(new Date(@_normalizeDateString(started)))
finished = if finished then @_toUtc(new Date(@_normalizeDateString(finished))) else @_nowUtc() finished = if finished then @_toUtc(new Date(@_normalizeDateString(finished))) else @_nowUtc()
if started && finished then Math.round((finished - started) / 1000) else 0 if started && finished then Math.round((finished - started) / 1000) else 0
timeInWords: (duration) -> timeInWords = (duration) ->
days = Math.floor(duration / 86400) days = Math.floor(duration / 86400)
hours = Math.floor(duration % 86400 / 3600) hours = Math.floor(duration % 86400 / 3600)
minutes = Math.floor(duration % 3600 / 60) minutes = Math.floor(duration % 3600 / 60)
@ -71,7 +80,7 @@ require 'config/emoij'
result.push seconds + ' sec' if seconds > 0 result.push seconds + ' sec' if seconds > 0
if result.length > 0 then result.join(' ') else '-' if result.length > 0 then result.join(' ') else '-'
githubify: (text, owner, repo) -> githubify = (text, owner, repo) ->
self = this self = this
text = text.replace @_githubReferenceRegexp, (reference, matchedOwner, matchedRepo, matchedNumber) -> text = text.replace @_githubReferenceRegexp, (reference, matchedOwner, matchedRepo, matchedNumber) ->
self._githubReferenceLink(reference, { owner: owner, repo: repo }, { owner: matchedOwner, repo: matchedRepo, number: matchedNumber } ) self._githubReferenceLink(reference, { owner: owner, repo: repo }, { owner: matchedOwner, repo: matchedRepo, number: matchedNumber } )
@ -81,51 +90,77 @@ require 'config/emoij'
self._githubCommitReferenceLink(reference, { owner: owner, repo: repo }, { owner: matchedOwner, repo: matchedRepo, sha: matchedSHA }) self._githubCommitReferenceLink(reference, { owner: owner, repo: repo }, { owner: matchedOwner, repo: matchedRepo, sha: matchedSHA })
text text
_githubReferenceRegexp: new RegExp("([\\w-]+)?\\/?([\\w-]+)?(?:#|gh-)(\\d+)", 'g') _githubReferenceRegexp = new RegExp("([\\w-]+)?\\/?([\\w-]+)?(?:#|gh-)(\\d+)", 'g')
_githubReferenceLink: (reference, current, matched) -> _githubReferenceLink = (reference, current, matched) ->
owner = matched.owner || current.owner owner = matched.owner || current.owner
repo = matched.repo || current.repo repo = matched.repo || current.repo
"<a href=\"#{Travis.config.source_endpoint}/#{owner}/#{repo}/issues/#{matched.number}\">#{reference}</a>" "<a href=\"#{config.source_endpoint}/#{owner}/#{repo}/issues/#{matched.number}\">#{reference}</a>"
_githubUserRegexp: new RegExp("\\B@([\\w-]+)", 'g') _githubUserRegexp = new RegExp("\\B@([\\w-]+)", 'g')
_githubUserLink: (reference, username) -> _githubUserLink = (reference, username) ->
"<a href=\"#{Travis.config.source_endpoint}/#{username}\">#{reference}</a>" "<a href=\"#{config.source_endpoint}/#{username}\">#{reference}</a>"
_githubCommitReferenceRegexp: new RegExp("([\\w-]+)?\\/([\\w-]+)?@([0-9A-Fa-f]+)", 'g') _githubCommitReferenceRegexp = new RegExp("([\\w-]+)?\\/([\\w-]+)?@([0-9A-Fa-f]+)", 'g')
_githubCommitReferenceLink: (reference, current, matched) -> _githubCommitReferenceLink = (reference, current, matched) ->
owner = matched.owner || current.owner owner = matched.owner || current.owner
repo = matched.repo || current.repo repo = matched.repo || current.repo
url = "#{Travis.Urls.githubCommit("#{owner}/#{repo}", matched.sha)}" url = "#{githubCommitUrl("#{owner}/#{repo}", matched.sha)}"
"<a href=\"#{url}\">#{reference}</a>" "<a href=\"#{url}\">#{reference}</a>"
_normalizeDateString: (string) -> _normalizeDateString = (string) ->
if window.JHW if window.JHW
string = string.replace('T', ' ').replace(/-/g, '/') string = string.replace('T', ' ').replace(/-/g, '/')
string = string.replace('Z', '').replace(/\..*$/, '') string = string.replace('Z', '').replace(/\..*$/, '')
string string
_nowUtc: -> _nowUtc = ->
@_toUtc Travis.currentDate() @_toUtc currentDate()
_toUtc: (date) -> _toUtc = (date) ->
Date.UTC date.getFullYear(), date.getMonth(), date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds(), date.getMilliseconds() Date.UTC date.getFullYear(), date.getMonth(), date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds(), date.getMilliseconds()
_emojize: (text) -> _emojize = (text) ->
emojis = text.match(/:\S+?:/g) emojis = text.match(/:\S+?:/g)
if emojis isnt null if emojis isnt null
$.each emojis.uniq(), (ix, emoji) -> emojis.uniq().each (emoji, ix) ->
strippedEmoji = emoji.substring(1, emoji.length - 1) strippedEmoji = emoji.substring(1, emoji.length - 1)
unless EmojiDictionary.indexOf(strippedEmoji) is -1 unless EmojiDictionary.indexOf(strippedEmoji) is -1
image = '<img class=\'emoji\' title=\'' + emoji + '\' alt=\'' + emoji + '\' src=\'' + '/images/emoji/' + strippedEmoji + '.png\'/>' image = '<img class=\'emoji\' title=\'' + emoji + '\' alt=\'' + emoji + '\' src=\'' + '/images/emoji/' + strippedEmoji + '.png\'/>'
text = text.replace(new RegExp(emoji, 'g'), image) text = text.replace(new RegExp(emoji, 'g'), image)
text text
_escape: (text) -> _escape = (text) ->
text.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace />/g, '&gt;' text.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace />/g, '&gt;'
configKeys: (config) -> configKeys = (config) ->
return [] unless config return [] unless config
$.intersect($.keys(config), Object.keys(Travis.CONFIG_KEYS_MAP)) intersect(Object.keys(config), Object.keys(config_keys_map))
Travis.Helpers =
configKeys: configKeys
_escape: _escape
_emojize: _emojize
_toUtc: _toUtc
_nowUtc: _nowUtc
_normalizeDateString: _normalizeDateString
_githubCommitReferenceLink: _githubCommitReferenceLink
_githubCommitReferenceRegexp: _githubCommitReferenceRegexp
_githubUserLink: _githubUserLink
_githubUserRegexp: _githubUserRegexp
_githubReferenceLink: _githubReferenceLink
_githubReferenceRegexp: _githubReferenceRegexp
githubify: githubify
timeInWords: timeInWords
durationFrom: durationFrom
timeAgoInWords: timeAgoInWords
pathFrom: pathFrom
formatMessage: formatMessage
formatConfig: formatConfig
formatSha: formatSha
formatCommit: formatCommit
colorForState: colorForState
safe: safe
compact: compact