Add content to template package

This commit is contained in:
Corey Johnson
2013-02-12 17:36:20 -08:00
parent 52d4723711
commit 156df69227
15 changed files with 126 additions and 9 deletions

View File

@@ -1,6 +1,7 @@
{View} = require 'space-pen'
Editor = require 'editor'
$ = require 'jquery'
_ = require 'underscore'
fs = require 'fs'
module.exports =
@@ -26,8 +27,9 @@ class PackageGeneratorView extends View
@miniEditor.setText(fs.join(config.userPackagesDirPath, placeholderName));
pathLength = @miniEditor.getText().length
@miniEditor.setSelectedBufferRange([[0, pathLength - placeholderName.length], [0, pathLength]])
@miniEditor.focus()
rootView.append(this)
@miniEditor.focus()
detach: ->
return unless @hasParent()
@@ -41,7 +43,9 @@ class PackageGeneratorView extends View
@detach()
getPackagePath: ->
@miniEditor.getText()
packagePath = @miniEditor.getText()
packageName = _.dasherize(fs.base(packagePath))
fs.join(fs.directory(packagePath), packageName)
validPackagePath: ->
if fs.exists(@getPackagePath())
@@ -58,11 +62,27 @@ class PackageGeneratorView extends View
for path in fs.listTree(templatePath)
relativePath = path.replace(templatePath, "")
relativePath = relativePath.replace(/^\//, '')
relativePath = relativePath.replace("__packageName__", packageName)
relativePath = @replacePackageNamePlaceholders(relativePath, packageName)
sourcePath = fs.join(@getPackagePath(), relativePath)
if fs.isDirectory(path)
fs.makeTree(sourcePath)
if fs.isFile(path)
fs.makeTree(fs.directory(sourcePath))
fs.write(sourcePath, fs.read(path))
content = @replacePackageNamePlaceholders(fs.read(path), packageName)
fs.write(sourcePath, content)
replacePackageNamePlaceholders: (string, packageName) ->
placeholderRegex = /##(?:(package-name)|([pP]ackageName)|(package_name))##/g
string = string.replace placeholderRegex, (match, dash, camel, underscore) ->
if dash
_.dasherize(packageName)
else if camel
if /[a-z]/.test(camel[0])
packageName = packageName[0].toLowerCase() + packageName[1...]
else if /[A-Z]/.test(camel[0])
packageName = packageName[0].toUpperCase() + packageName[1...]
_.camelize(packageName)
else if underscore
_.underscore(packageName)

View File

@@ -42,18 +42,29 @@ describe 'Package Generator', ->
@addMatchers
toExistOnDisk: (expected) ->
notText = this.isNot and " not" or ""
@message = -> return "Expected path '" + @actual + notText + "' to exist."
@message = -> return "Expected path '" + @actual + "'" + notText + " to exist."
fs.exists(@actual)
afterEach ->
fs.remove(packagePath) if fs.exists(packagePath)
it "forces the package's name to be lowercase with dashes", ->
packageName = "CamelCaseIsForTheBirds"
packagePath = fs.join(fs.directory(packagePath), packageName)
rootView.trigger("package-generator:generate")
packageGeneratorView = rootView.find(".package-generator").view()
packageGeneratorView.miniEditor.setText(packagePath)
packageGeneratorView.trigger "core:confirm"
expect(packagePath).not.toExistOnDisk()
expect(fs.join(fs.directory(packagePath), "camel-case-is-for-the-birds")).toExistOnDisk()
it "correctly lays out the package files and closes the package generator view", ->
rootView.trigger("package-generator:generate")
packageGeneratorView = rootView.find(".package-generator").view()
expect(packageGeneratorView.hasParent()).toBeTruthy()
packageGeneratorView.miniEditor.setText(packagePath)
packageGeneratorView.miniEditor.trigger "core:confirm"
packageGeneratorView.trigger "core:confirm"
expect("#{packagePath}/package.cson").toExistOnDisk()
expect("#{packagePath}/lib/#{packageName}.coffee").toExistOnDisk()
@@ -66,6 +77,20 @@ describe 'Package Generator', ->
expect(packageGeneratorView.hasParent()).toBeFalsy()
expect(rootView.getActiveEditor().isFocused).toBeTruthy()
it "replaces instances of packageName placeholders in template files", ->
rootView.trigger("package-generator:generate")
packageGeneratorView = rootView.find(".package-generator").view()
expect(packageGeneratorView.hasParent()).toBeTruthy()
packageGeneratorView.miniEditor.setText(packagePath)
packageGeneratorView.trigger "core:confirm"
lines = fs.read("#{packagePath}/package.cson").split("\n")
expect(lines[0]).toBe "'main': 'lib\/#{packageName}'"
lines = fs.read("#{packagePath}/lib/#{packageName}.coffee").split("\n")
expect(lines[0]).toBe "SweetPackageDudeView = require 'sweet-package-dude/lib/sweet-package-dude-view'"
expect(lines[3]).toBe " sweetPackageDudeView: null"
it "displays an error when the package path already exists", ->
rootView.attachToDom()
fs.makeTree(packagePath)
@@ -75,7 +100,7 @@ describe 'Package Generator', ->
expect(packageGeneratorView.hasParent()).toBeTruthy()
expect(packageGeneratorView.error).not.toBeVisible()
packageGeneratorView.miniEditor.setText(packagePath)
packageGeneratorView.miniEditor.trigger "core:confirm"
packageGeneratorView.trigger "core:confirm"
expect(packageGeneratorView.hasParent()).toBeTruthy()
expect(packageGeneratorView.error).toBeVisible()
@@ -86,5 +111,3 @@ describe 'Package Generator', ->
packageGeneratorView.trigger "core:confirm"
expect(atom.open).toHaveBeenCalledWith(packagePath)

View File

@@ -0,0 +1,3 @@
# DOCUMENT: link to keymap documentation
'body':
'meta-alt-ctrl-o': '##package-name##:toggle'

View File

@@ -0,0 +1,25 @@
{$$, View} = require 'space-pen'
module.exports =
class ##PackageName##View extends View
@content: ->
@div class: '##package-name## overlay from-top', =>
@div "The ##PackageName## package is Alive! It's ALIVE!", class: "message"
initialize: (serializeState) ->
rootView.command "##package-name##:toggle", => @toggle()
# Returns an object that can be retrieved when package is activated
serialize: ->
# Tear down any state and detach
destroy: ->
@detach()
toggle: ->
console.log "##PackageName##View was toggled!"
if @hasParent()
@detach()
else
rootView.append(this)

View File

@@ -0,0 +1,13 @@
##PackageName##View = require '##package-name##/lib/##package-name##-view'
module.exports =
##packageName##View: null
activate: (state) ->
@##packageName##View = new ##PackageName##View(state.##packageName##ViewState)
deactivate: ->
@##packageName##View.destroy()
serialize: ->
##packageName##ViewState: @##packageName##View.serialize()

View File

@@ -0,0 +1,2 @@
'main': 'lib/##package-name##'
'activationEvents': ['##package-name##:toggle']

View File

@@ -0,0 +1,5 @@
##PackageName## = require '##package-name##/lib/##package-name##'
describe "##PackageName##", ->
it "has one valid test", ->
expect("life").toBe "easy"

View File

@@ -0,0 +1,24 @@
##PackageName##View = require '##package-name##/lib/##package-name##-view'
RootView = require 'root-view'
# This spec is focused because it starts with an `f`. Remove the `f`
# to unfocus the spec.
#
# Press meta-alt-ctrl-s to run the specs
fdescribe "##PackageName##View", ->
##packageName## = null
beforeEach ->
new RootView()
##packageName## = atom.loadPackage('##packageName##', activateImmediately: true)
afterEach ->
rootView.deactivate()
describe "when the ##package-name##:toggle event is triggered", ->
it "attaches and then detaches the view", ->
expect(rootView.find('.##package-name##')).not.toExist()
rootView.trigger '##package-name##:toggle'
expect(rootView.find('.##package-name##')).toExist()
rootView.trigger '##package-name##:toggle'
expect(rootView.find('.##package-name##')).not.toExist()

View File

@@ -0,0 +1,2 @@
.##package-name## {
}