forked from desktop/desktop
-
Notifications
You must be signed in to change notification settings - Fork 527
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
introduce abstractions to make Flatpak integration easier (#555)
Co-Authored-By: nullrequest <30698906+advaithm@users.noreply.github.com>
- Loading branch information
1 parent
db9b000
commit 4c2757a
Showing
7 changed files
with
190 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,11 @@ | ||
export interface IFoundEditor<T> { | ||
readonly editor: T | ||
readonly path: string | ||
/** | ||
* Indicate to Desktop to launch the editor with the `shell: true` option included. | ||
* | ||
* This is available to all platforms, but is only currently used by some Windows | ||
* editors as their launch programs end in `.cmd` | ||
*/ | ||
readonly usesShell?: boolean | ||
} |
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,93 @@ | ||
import { join } from 'path' | ||
import { pathExists as pathExistsInternal } from 'fs-extra' | ||
import { | ||
ChildProcess, | ||
spawn, | ||
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) | ||
} | ||
|
||
/** | ||
* 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 spawnShell( | ||
path: string, | ||
args: string[], | ||
options?: SpawnOptionsWithoutStdio | ||
): ChildProcess { | ||
if (isFlatpakBuild()) { | ||
return spawn('flatpak-spawn', ['--host', path, ...args], options) | ||
} | ||
|
||
return spawn(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()) { | ||
return spawn( | ||
'flatpak-spawn', | ||
['--host', path, `"${workingDirectory}"`], | ||
options | ||
) | ||
} else { | ||
return spawn(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,30 @@ | ||
import { convertToFlatpakPath } 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) | ||
}) | ||
} | ||
}) |