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