Implement Portable Mode

According to these specifications:
Portable mode only applies to Windows (for now)
Portable mode only applies if ATOM_HOME is not set
If there is a .atom directory as sibling to directory with running process
	use that as ATOM_HOME
This commit is contained in:
Dave Rael
2015-10-09 10:54:30 -06:00
parent f067fdb7cd
commit b8a153781e
3 changed files with 71 additions and 14 deletions

View File

@@ -0,0 +1,52 @@
path = require "path"
fs = require 'fs-plus'
temp = require "temp"
rimraf = require "rimraf"
AtomPortable = require "../src/browser/atom-portable"
fdescribe "Portable Mode", ->
describe "Windows", ->
platform = "win32"
describe "with ATOM_HOME environment variable", ->
environmentAtomHome = "C:\\some\\path"
it "returns false", ->
expect(AtomPortable.isPortableInstall(platform, environmentAtomHome)).toBe false
describe "without ATOM_HOME environment variable", ->
environmentAtomHome = undefined
portableAtomHomePath = path.join(path.dirname(process.execPath), "../.atom").toString()
portableAtomHomeNaturallyExists = fs.existsSync(portableAtomHomePath)
portableAtomHomeBackupPath = portableAtomHomePath + ".temp"
beforeEach ->
fs.renameSync(portableAtomHomePath, portableAtomHomeBackupPath) if fs.existsSync(portableAtomHomePath)
afterEach ->
if portableAtomHomeNaturallyExists
fs.renameSync(portableAtomHomeBackupPath, portableAtomHomePath) if not fs.existsSync(portableAtomHomePath)
else
rimraf.sync(portableAtomHomePath) if fs.existsSync(portableAtomHomePath)
rimraf.sync(portableAtomHomeBackupPath) if fs.existsSync(portableAtomHomeBackupPath)
describe "with .atom directory sibling to exec", ->
beforeEach ->
fs.mkdirSync(portableAtomHomePath) if not fs.existsSync(portableAtomHomePath)
it "returns true", ->
expect(AtomPortable.isPortableInstall(platform, environmentAtomHome)).toBe true
describe "without .atom directory sibling to exec", ->
beforeEach ->
rimraf.sync(portableAtomHomePath) if fs.existsSync(portableAtomHomePath)
it "returns false", ->
expect(AtomPortable.isPortableInstall(platform, environmentAtomHome)).toBe false
describe "Mac", ->
platform = "darwin"
it "returns false", ->
expect(AtomPortable.isPortableInstall(platform, platform)).toBe false
describe "Linux", ->
platform = "linux"
it "returns false", ->
expect(AtomPortable.isPortableInstall(platform, platform)).toBe false

View File

@@ -0,0 +1,15 @@
fs = require 'fs-plus'
path = require 'path'
module.exports =
class AtomPortable
@portableAtomHomePath: ->
execDirectoryPath = path.dirname(process.execPath)
return path.join(execDirectoryPath, "../.atom")
@isPortableInstall: (platform, environmentAtomHome) ->
return false unless platform is 'win32'
return false if environmentAtomHome
# currently checking only that the directory exists, probably want to do
# some integrity checks on contents and make sure it's writable in future
return fs.existsSync(@portableAtomHomePath())

View File

@@ -57,23 +57,13 @@ handleStartupEventWithSquirrel = ->
setupCrashReporter = ->
crashReporter.start(productName: 'Atom', companyName: 'GitHub')
isPortableInstall = (args) ->
return false unless process.platform is 'win32'
return false unless (process and process.type)
return false if args.devMode or args.testMode
ourPath = process.execPath.toLowerCase()
return (ourPath.indexOf(process.env.LOCALAPPDATA.toLowerCase()) is 0)
setupAtomHome = (args) ->
return if process.env.ATOM_HOME
atomHome = path.join(app.getHomeDir(), '.atom')
try
atomHome = fs.realpathSync(atomHome)
fs.statSync(atomHome)
catch
atomHome = path.join(path.dirname(process.execPath), '.atom') if isPortableInstall(args)
AtomPortable = require './atom-portable'
atomHome = AtomPortable.portableAtomHomePath() if AtomPortable.isPortableInstall process.platform, process.env.ATOM_HOME
atomHome = fs.realpathSync(atomHome)
fs.statSync(atomHome)
process.env.ATOM_HOME = atomHome