See #864. Add Migration command from old option keys to new keys

Old:
    { LANG_OPTION: 'VALUE' }
New:
    { LANG: { OPTION: 'VALUE' }}
This commit is contained in:
Glavin Wiechert 2016-04-01 16:57:30 -03:00
parent fa9dd8dd4f
commit afa27fd50b
6 changed files with 83 additions and 11 deletions

View File

@ -121,6 +121,7 @@
"atom-workspace": [ "atom-workspace": [
"atom-beautify:help-debug-editor", "atom-beautify:help-debug-editor",
"atom-beautify:beautify-editor", "atom-beautify:beautify-editor",
"atom-beautify:migrate-settings",
"core:save", "core:save",
"core:save-as" "core:save-as"
], ],

View File

@ -198,6 +198,25 @@ describe "Atom-Beautify", ->
editor = e editor = e
expect(editor.getText()).toEqual("") expect(editor.getText()).toEqual("")
describe "Migrate Settings", ->
migrateSettings = (beforeKey, afterKey, val) ->
# set old options
atom.config.set("atom-beautify.#{beforeKey}", val)
atom.commands.dispatch workspaceElement, "atom-beautify:migrate-settings"
# Check resulting config
expect(_.has(atom.config.get('atom-beautify'), beforeKey)).toBe(false)
expect(atom.config.get("atom-beautify.#{afterKey}")).toBe(val)
it "should migrate js_indent_size to js.indent_size", ->
migrateSettings("js_indent_size","js.indent_size", 10)
it "should migrate analytics to general.analytics", ->
migrateSettings("analytics","general.analytics", true)
it "should migrate _analyticsUserId to general._analyticsUserId", ->
migrateSettings("_analyticsUserId","general._analyticsUserId", "userid")
beautifyEditor = (callback) -> beautifyEditor = (callback) ->
isComplete = false isComplete = false
beforeText = null beforeText = null
@ -238,9 +257,9 @@ describe "Atom-Beautify", ->
# See https://discuss.atom.io/t/solved-settimeout-not-working-firing-in-specs-tests/11427/17 # See https://discuss.atom.io/t/solved-settimeout-not-working-firing-in-specs-tests/11427/17
jasmine.unspy(window, 'setTimeout') jasmine.unspy(window, 'setTimeout')
afterEach -> # afterEach ->
atom.packages.deactivatePackages() # atom.packages.deactivatePackages()
atom.packages.unloadPackages() # atom.packages.unloadPackages()
describe ".jsbeautifyrc", -> describe ".jsbeautifyrc", ->
@ -249,7 +268,7 @@ describe "Atom-Beautify", ->
getOptions = (callback) -> getOptions = (callback) ->
options = null options = null
waitsForPromise -> waitsForPromise ->
console.log('beautifier', beautifier.getOptionsForPath, beautifier) # console.log('beautifier', beautifier.getOptionsForPath, beautifier)
allOptions = beautifier.getOptionsForPath(null, null) allOptions = beautifier.getOptionsForPath(null, null)
# Resolve options with promises # Resolve options with promises
return Promise.all(allOptions) return Promise.all(allOptions)
@ -308,7 +327,7 @@ describe "Languages", ->
namespaceGroups = _.groupBy(languages.languages, "namespace") namespaceGroups = _.groupBy(languages.languages, "namespace")
namespacePairs = _.toPairs(namespaceGroups) namespacePairs = _.toPairs(namespaceGroups)
namespaceOverlap = _.filter(namespacePairs, ([namespace, group]) -> group.length > 1) namespaceOverlap = _.filter(namespacePairs, ([namespace, group]) -> group.length > 1)
console.log('namespaces', namespaceGroups, namespacePairs, namespaceOverlap) # console.log('namespaces', namespaceGroups, namespacePairs, namespaceOverlap)
expect(namespaceOverlap.length).toBe(0, \ expect(namespaceOverlap.length).toBe(0, \
"Language namespaces are overlapping.\n\ "Language namespaces are overlapping.\n\
Namespaces are unique: only one language for each namespace.\n"+ Namespaces are unique: only one language for each namespace.\n"+

View File

@ -15,7 +15,7 @@ isWindows = process.platform is 'win32' or
process.env.OSTYPE is 'cygwin' or process.env.OSTYPE is 'cygwin' or
process.env.OSTYPE is 'msys' process.env.OSTYPE is 'msys'
xdescribe "BeautifyLanguages", -> describe "BeautifyLanguages", ->
optionsDir = path.resolve(__dirname, "../examples") optionsDir = path.resolve(__dirname, "../examples")

View File

@ -286,16 +286,16 @@ module.exports = class Beautifiers extends EventEmitter
# Setup Analytics # Setup Analytics
analytics = new Analytics(analyticsWriteKey) analytics = new Analytics(analyticsWriteKey)
unless atom.config.get("atom-beautify.general.analyticsUserId") unless atom.config.get("atom-beautify.general._analyticsUserId")
uuid = require("node-uuid") uuid = require("node-uuid")
atom.config.set "atom-beautify.general.analyticsUserId", uuid.v4() atom.config.set "atom-beautify.general._analyticsUserId", uuid.v4()
# Setup Analytics User Id # Setup Analytics User Id
userId = atom.config.get("atom-beautify.general.analyticsUserId") userId = atom.config.get("atom-beautify.general._analyticsUserId")
analytics.identify userId : userId analytics.identify userId : userId
version = pkg.version version = pkg.version
analytics.track analytics.track
userId : atom.config.get("atom-beautify.general.analyticsUserId") userId : atom.config.get("atom-beautify.general._analyticsUserId")
event : "Beautify" event : "Beautify"
properties : properties :
language : language?.name language : language?.name

View File

@ -67,6 +67,8 @@ showError = (error) ->
beautify = ({onSave}) -> beautify = ({onSave}) ->
plugin.checkUnsupportedOptions()
# Continue beautifying # Continue beautifying
path ?= require("path") path ?= require("path")
forceEntireFile = onSave and atom.config.get("atom-beautify.general.beautifyEntireFileOnSave") forceEntireFile = onSave and atom.config.get("atom-beautify.general.beautifyEntireFileOnSave")
@ -259,6 +261,8 @@ beautifyDirectory = ({target}) ->
debug = () -> debug = () ->
plugin.checkUnsupportedOptions()
# Get current editor # Get current editor
editor = atom.workspace.getActiveTextEditor() editor = atom.workspace.getActiveTextEditor()
@ -497,6 +501,53 @@ handleSaveEvent = ->
) )
) )
plugin.subscriptions.add disposable plugin.subscriptions.add disposable
getUnsupportedOptions = ->
settings = atom.config.get('atom-beautify')
schema = atom.config.getSchema('atom-beautify')
unsupportedOptions = _.filter(_.keys(settings), (key) ->
# return atom.config.getSchema("atom-beautify.${key}").type
# return typeof settings[key]
schema.properties[key] is undefined
)
return unsupportedOptions
plugin.checkUnsupportedOptions = ->
unsupportedOptions = getUnsupportedOptions()
if unsupportedOptions.length isnt 0
atom.notifications.addWarning("You have unsupported options: #{unsupportedOptions.join(', ')} <br> Please run Atom command 'Atom-Beautify: Migrate Settings'.")
plugin.migrateSettings = ->
unsupportedOptions = getUnsupportedOptions()
namespaces = beautifier.languages.namespaces
# console.log('migrate-settings', schema, namespaces, unsupportedOptions)
if unsupportedOptions.length is 0
atom.notifications.addSuccess("No options to migrate.")
else
rex = new RegExp("(#{namespaces.join('|')})_(.*)")
rename = _.toPairs(_.zipObject(unsupportedOptions, _.map(unsupportedOptions, (key) ->
m = key.match(rex)
if m is null
# Did not match
# Put into general
return "general.#{key}"
else
return "#{m[1]}.#{m[2]}"
)))
# console.log('rename', rename)
# logger.verbose('rename', rename)
# Move all option values to renamed key
_.each(rename, ([key, newKey]) ->
# console.log('rename', key, newKey)
# Copy to new key
val = atom.config.get("atom-beautify.#{key}")
atom.config.set("atom-beautify.#{newKey}", val)
# Delete old key
atom.config.set("atom-beautify.#{key}", undefined)
)
atom.notifications.addSuccess("Successfully migrated options: #{unsupportedOptions.join(', ')}")
plugin.config = _.merge(require('./config.coffee'), defaultLanguageOptions) plugin.config = _.merge(require('./config.coffee'), defaultLanguageOptions)
plugin.activate = -> plugin.activate = ->
@subscriptions = new CompositeDisposable @subscriptions = new CompositeDisposable
@ -505,6 +556,7 @@ plugin.activate = ->
@subscriptions.add atom.commands.add "atom-workspace", "atom-beautify:help-debug-editor", debug @subscriptions.add atom.commands.add "atom-workspace", "atom-beautify:help-debug-editor", debug
@subscriptions.add atom.commands.add ".tree-view .file .name", "atom-beautify:beautify-file", beautifyFile @subscriptions.add atom.commands.add ".tree-view .file .name", "atom-beautify:beautify-file", beautifyFile
@subscriptions.add atom.commands.add ".tree-view .directory .name", "atom-beautify:beautify-directory", beautifyDirectory @subscriptions.add atom.commands.add ".tree-view .directory .name", "atom-beautify:beautify-directory", beautifyDirectory
@subscriptions.add atom.commands.add "atom-workspace", "atom-beautify:migrate-settings", plugin.migrateSettings
plugin.deactivate = -> plugin.deactivate = ->
@subscriptions.dispose() @subscriptions.dispose()

View File

@ -15,7 +15,7 @@ module.exports = {
used the most, as well as other stats. Everything is anonymized and no personal used the most, as well as other stats. Everything is anonymized and no personal
information, such as source code, is sent. information, such as source code, is sent.
See https://github.com/Glavin001/atom-beautify/issues/47 for more details." See https://github.com/Glavin001/atom-beautify/issues/47 for more details."
analyticsUserId : _analyticsUserId :
title: 'Analytics User Id' title: 'Analytics User Id'
type : 'string' type : 'string'
default : "" default : ""