Skip to content

Commit

Permalink
Merge pull request #207 from pulsar-edit/bundle-package-generator
Browse files Browse the repository at this point in the history
Bundle `package-generator`
  • Loading branch information
confused-Techie authored Dec 11, 2022
2 parents 8dac211 + aebabc9 commit 05b3976
Show file tree
Hide file tree
Showing 14 changed files with 879 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@
"one-light-syntax": "file:packages/one-light-syntax",
"one-light-ui": "file:packages/one-light-ui",
"open-on-github": "https://codeload.github.com/atom/open-on-github/legacy.tar.gz/refs/tags/v1.3.2",
"package-generator": "https://codeload.github.com/atom/package-generator/legacy.tar.gz/refs/tags/v1.3.0",
"package-generator": "file:packages/package-generator",
"pathwatcher": "^8.1.2",
"postcss": "8.2.10",
"postcss-selector-parser": "6.0.4",
Expand Down
3 changes: 1 addition & 2 deletions packages/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ See [RFC 003](https://github.com/atom/atom/blob/master/docs/rfcs/003-consolidate
| **one-light-syntax** | [`./one-light-syntax`](./one-light-syntax) | |
| **one-light-ui** | [`./one-light-ui`](./one-light-ui) | |
| **open-on-github** | [`atom/open-on-github`][open-on-github] | [#18278](https://github.com/atom/atom/issues/18278) |
| **package-generator** | [`atom/package-generator`][package-generator] | [#18279](https://github.com/atom/atom/issues/18279) |
| **package-generator** | [`./package-generator`][./package-generator] | |
| **settings-view** | [`atom/settings-view`][settings-view] | |
| **snippets** | [`atom/snippets`][snippets] | |
| **solarized-dark-syntax** | [`./solarized-dark-syntax`](./solarized-dark-syntax) | |
Expand Down Expand Up @@ -120,7 +120,6 @@ See [RFC 003](https://github.com/atom/atom/blob/master/docs/rfcs/003-consolidate
[markdown-preview]: https://github.com/pulsar-edit/markdown-preview
[notifications]: https://github.com/pulsar-edit/notifications
[open-on-github]: https://github.com/pulsar-edit/open-on-github
[package-generator]: https://github.com/pulsar-edit/package-generator
[settings-view]: https://github.com/pulsar-edit/settings-view
[snippets]: https://github.com/pulsar-edit/snippets
[spell-check]: https://github.com/pulsar-edit/spell-check
Expand Down
1 change: 1 addition & 0 deletions packages/package-generator/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
1 change: 1 addition & 0 deletions packages/package-generator/CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[See how you can contribute](https://github.com/pulsar-edit/.github/blob/main/CONTRIBUTING.md)
3 changes: 3 additions & 0 deletions packages/package-generator/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Package Generator package

Generates and opens a new sample package, language, or syntax theme in Pulsar.
11 changes: 11 additions & 0 deletions packages/package-generator/lib/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const PackageGeneratorView = require('./package-generator-view')

module.exports = {
activate () {
this.view = new PackageGeneratorView()
},

deactivate () {
if (this.view) this.view.destroy()
}
}
149 changes: 149 additions & 0 deletions packages/package-generator/lib/package-generator-view.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
const path = require('path')
const _ = require('underscore-plus')
const {TextEditor, BufferedProcess, CompositeDisposable, Disposable} = require('atom')
const fs = require('fs-plus')

module.exports =
class PackageGeneratorView {
constructor () {
this.disposables = new CompositeDisposable()

this.element = document.createElement('div')
this.element.classList.add('package-generator')

this.miniEditor = new TextEditor({mini: true})
this.element.appendChild(this.miniEditor.element)

this.error = document.createElement('div')
this.error.classList.add('error')
this.element.appendChild(this.error)

this.message = document.createElement('div')
this.message.classList.add('message')
this.element.appendChild(this.message)

this.disposables.add(atom.commands.add('atom-workspace', {
'package-generator:generate-package': () => this.attach('package'),
'package-generator:generate-language-package': () => this.attach('language'),
'package-generator:generate-syntax-theme': () => this.attach('theme')
}))

const blurHandler = () => this.close()
this.miniEditor.element.addEventListener('blur', blurHandler)
this.disposables.add(new Disposable(() => this.miniEditor.element.removeEventListener('blur', blurHandler)))
this.disposables.add(atom.commands.add(this.element, {
'core:confirm': () => this.confirm(),
'core:cancel': () => this.close()
}))
}

destroy () {
if (this.panel != null) this.panel.destroy()
this.disposables.dispose()
}

attach (mode) {
this.mode = mode
if (this.panel == null) this.panel = atom.workspace.addModalPanel({item: this, visible: false})
this.previouslyFocusedElement = document.activeElement
this.panel.show()
this.message.textContent = `Enter ${this.mode} path`
if (this.mode === 'package') {
this.setPathText('my-package')
} else if (this.mode === 'language') {
this.setPathText('language-my-language', [9, Infinity])
} else {
this.setPathText('my-theme-syntax', [0, 8])
}
this.miniEditor.element.focus()
}

setPathText (placeholderName, rangeToSelect) {
if (rangeToSelect == null) rangeToSelect = [0, placeholderName.length]
const packagesDirectory = this.getPackagesDirectory()
this.miniEditor.setText(path.join(packagesDirectory, placeholderName))
const pathLength = this.miniEditor.getText().length
const endOfDirectoryIndex = pathLength - placeholderName.length
this.miniEditor.setSelectedBufferRange([[0, endOfDirectoryIndex + rangeToSelect[0]], [0, endOfDirectoryIndex + rangeToSelect[1]]])
}

close () {
if (!this.panel.isVisible()) return
this.panel.hide()
if (this.previouslyFocusedElement != null) this.previouslyFocusedElement.focus()
}

confirm () {
if (this.validPackagePath()) {
this.createPackageFiles(() => {
const packagePath = this.getPackagePath()
atom.open({pathsToOpen: [packagePath]})
this.close()
})
}
}

getPackagePath () {
const packagePath = fs.normalize(this.miniEditor.getText().trim())
const packageName = _.dasherize(path.basename(packagePath))
return path.join(path.dirname(packagePath), packageName)
}

getPackagesDirectory () {
return process.env.ATOM_REPOS_HOME || atom.config.get('core.projectHome') || path.join(fs.getHomeDirectory(), 'github')
}

validPackagePath () {
if (fs.existsSync(this.getPackagePath())) {
this.error.textContent = `Path already exists at '${this.getPackagePath()}'`
this.error.style.display = 'block'
return false
} else {
return true
}
}

getInitOptions (packagePath) {
const options = [`--${this.mode}`, packagePath]
if (this.mode !== 'theme') {
return [...options, '--syntax', atom.config.get('package-generator.packageSyntax')]
} else {
return options
}
}

initPackage (packagePath, callback) {
const command = ['init'].concat(this.getInitOptions(packagePath))
this.runCommand(atom.packages.getApmPath(), command, callback)
}

linkPackage (packagePath, callback) {
const args = ['link']
if (atom.config.get('package-generator.createInDevMode')) args.push('--dev')
args.push(packagePath.toString())

this.runCommand(atom.packages.getApmPath(), args, callback)
}

isStoredInDotAtom (packagePath) {
const packagesPath = path.join(atom.getConfigDirPath(), 'packages', path.sep)
if (packagePath.startsWith(packagesPath)) return true

const devPackagesPath = path.join(atom.getConfigDirPath(), 'dev', 'packages', path.sep)
return packagePath.startsWith(devPackagesPath)
}

createPackageFiles (callback) {
const packagePath = this.getPackagePath()

if (this.isStoredInDotAtom(packagePath)) {
this.initPackage(packagePath, callback)
} else {
this.initPackage(packagePath, () => this.linkPackage(packagePath, callback))
}
}

runCommand (command, args, exit) {
this.process = new BufferedProcess({command, args, exit})
}
}
10 changes: 10 additions & 0 deletions packages/package-generator/menus/package-generator.cson
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'menu': [
'label': 'Packages'
'submenu': [
'label': 'Package Generator'
'submenu': [
{ 'label': 'Generate Package', 'command': 'package-generator:generate-package' }
{ 'label': 'Generate Syntax Theme', 'command': 'package-generator:generate-syntax-theme' }
]
]
]
Loading

0 comments on commit 05b3976

Please sign in to comment.