forked from desktop/desktop
-
Notifications
You must be signed in to change notification settings - Fork 529
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(tooling): flatpak - introduce abstractions to make Flatpak integ…
- Loading branch information
1 parent
b454a70
commit 6af9fb0
Showing
6 changed files
with
153 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import { join } from 'path' | ||
import { pathExists as pathExistsInternal } from 'fs-extra' | ||
import { | ||
ChildProcess, | ||
spawn as nodeSpawn, | ||
SpawnOptionsWithoutStdio, | ||
SpawnOptions, | ||
} from 'child_process' | ||
|
||
export function isFlatpakBuild() { | ||
return __LINUX__ && process.env.FLATPAK_HOST === '1' | ||
} | ||
|
||
/** | ||
* Convert an executable path to be relative to the flatpak host | ||
* | ||
* @param path a path to an executable relative to the root of the filesystem | ||
*/ | ||
export function convertToFlatpakPath(path: string) { | ||
if (!__LINUX__) { | ||
return path | ||
} | ||
|
||
if (path.startsWith('/opt/')) { | ||
return path | ||
} | ||
|
||
return join('/var/run/host', path) | ||
} | ||
|
||
export function formatWorkingDirectoryForFlatpak(path: string): string { | ||
return path.replace(/(\s)/, ' ') | ||
} | ||
|
||
/** | ||
* Checks the file path on disk exists before attempting to launch a specific shell | ||
* | ||
* @param path | ||
* | ||
* @returns `true` if the path can be resolved, or `false` otherwise | ||
*/ | ||
export async function pathExists(path: string): Promise<boolean> { | ||
if (isFlatpakBuild()) { | ||
path = convertToFlatpakPath(path) | ||
} | ||
|
||
try { | ||
return await pathExistsInternal(path) | ||
} catch { | ||
return false | ||
} | ||
} | ||
|
||
/** | ||
* Spawn a particular shell in a way that works for Flatpak-based usage | ||
* | ||
* @param path path to shell, relative to the root of the filesystem | ||
* @param args arguments to provide to the shell | ||
* @param options additional options to provide to spawn function | ||
* | ||
* @returns a child process to observe and monitor | ||
*/ | ||
export function spawn( | ||
path: string, | ||
args: ReadonlyArray<string>, | ||
options?: SpawnOptionsWithoutStdio | ||
): ChildProcess { | ||
if (isFlatpakBuild()) { | ||
return nodeSpawn('flatpak-spawn', ['--host', path, ...args], options) | ||
} | ||
|
||
return nodeSpawn(path, args, options) | ||
} | ||
|
||
/** | ||
* Spawn a given editor in a way that works for Flatpak-based usage | ||
* | ||
* @param path path to editor, relative to the root of the filesystem | ||
* @param workingDirectory working directory to open initially in editor | ||
* @param options additional options to provide to spawn function | ||
*/ | ||
export function spawnEditor( | ||
path: string, | ||
workingDirectory: string, | ||
options: SpawnOptions | ||
): ChildProcess { | ||
if (isFlatpakBuild()) { | ||
const EscapedworkingDirectory = | ||
formatWorkingDirectoryForFlatpak(workingDirectory) | ||
return nodeSpawn( | ||
'flatpak-spawn', | ||
['--host', path, EscapedworkingDirectory], | ||
options | ||
) | ||
} else { | ||
return nodeSpawn(path, [workingDirectory], options) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { | ||
convertToFlatpakPath, | ||
formatWorkingDirectoryForFlatpak, | ||
} from '../../../src/lib/helpers/linux' | ||
|
||
describe('convertToFlatpakPath()', () => { | ||
if (__LINUX__) { | ||
it('converts /usr paths', () => { | ||
const path = '/usr/bin/subl' | ||
const expectedPath = '/var/run/host/usr/bin/subl' | ||
expect(convertToFlatpakPath(path)).toEqual(expectedPath) | ||
}) | ||
|
||
it('preserves /opt paths', () => { | ||
const path = '/opt/slickedit-pro2018/bin/vs' | ||
expect(convertToFlatpakPath(path)).toEqual(path) | ||
}) | ||
} | ||
|
||
if (__WIN32__) { | ||
it('returns same path', () => { | ||
const path = 'C:\\Windows\\System32\\Notepad.exe' | ||
expect(convertToFlatpakPath(path)).toEqual(path) | ||
}) | ||
} | ||
|
||
if (__DARWIN__) { | ||
it('returns same path', () => { | ||
const path = '/usr/local/bin/code' | ||
expect(convertToFlatpakPath(path)).toEqual(path) | ||
}) | ||
} | ||
}) | ||
|
||
describe('formatWorkingDirectoryForFlatpak()', () => { | ||
if (__LINUX__) { | ||
it('escapes string', () => { | ||
const path = '/home/test/path with space' | ||
const expectedPath = '/home/test/path with space' | ||
expect(formatWorkingDirectoryForFlatpak(path)).toEqual(expectedPath) | ||
}) | ||
it('returns same path', () => { | ||
const path = '/home/test/path_wthout_spaces' | ||
expect(formatWorkingDirectoryForFlatpak(path)).toEqual(path) | ||
}) | ||
} | ||
}) |