generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch '#76-os-specific-commands' into 0.7.0
- Loading branch information
Showing
18 changed files
with
759 additions
and
280 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
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,22 @@ | ||
import {extractFileName, getOperatingSystem, isWindows} from "./Common"; | ||
import {PlatformShells} from "./settings/ShellCommandsPluginSettings"; | ||
|
||
export function getUsersDefaultShell(): string { | ||
if (isWindows()) { | ||
return process.env.ComSpec; | ||
} else { | ||
return process.env.SHELL; | ||
} | ||
} | ||
|
||
export function isShellSupported(shell: string) { | ||
const shell_file_name = extractFileName(shell); | ||
const supported_shells = PlatformShells[getOperatingSystem()]; | ||
for (let supported_shell_path in supported_shells) { | ||
if (supported_shell_path.substr(-shell_file_name.length, shell_file_name.length).toLowerCase() === shell_file_name.toLowerCase()) { | ||
// If supported_shell_path (e.g. /bin/bash or CMD.EXE) ends with shell_file_name (e.g. bash, derived from /bin/bash or CMD.EXE, derived from C:\System32\CMD.EXE), then the shell can be considered to be supported. | ||
return true; | ||
} | ||
} | ||
return false; | ||
} |
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,104 @@ | ||
import {ShellCommandConfiguration} from "./settings/ShellCommandConfiguration"; | ||
import ShellCommandsPlugin from "./main"; | ||
import {getOperatingSystem} from "./Common"; | ||
|
||
export interface TShellCommandContainer { | ||
[key: string]: TShellCommand, | ||
} | ||
|
||
export class TShellCommand { | ||
|
||
private readonly id: string; | ||
private plugin: ShellCommandsPlugin; | ||
private configuration: ShellCommandConfiguration; | ||
|
||
constructor (plugin: ShellCommandsPlugin, shell_command_id: string, configuration: ShellCommandConfiguration) { | ||
this.plugin = plugin; | ||
this.id = shell_command_id; | ||
this.configuration = configuration; | ||
} | ||
|
||
public getPlugin() { | ||
return this.plugin; | ||
} | ||
/** | ||
* Use this when you need to alter the configuration values. if you only need to read configuration values, use get*() | ||
* methods instead. | ||
*/ | ||
public getConfiguration() { | ||
return this.configuration; | ||
} | ||
|
||
public getId() { | ||
return this.id; | ||
} | ||
|
||
public getShell(): string { | ||
let operating_system = getOperatingSystem(); | ||
|
||
// Check if the shell command has defined a specific shell. | ||
if (undefined === this.configuration.shells[operating_system]) { | ||
// The shell command does not define an explicit shell. | ||
// Use a default shell from the plugin's settings. | ||
return this.plugin.getDefaultShell(); | ||
} else { | ||
// The shell command has an explicit shell defined. | ||
return this.configuration.shells[operating_system]; | ||
} | ||
} | ||
|
||
public getShells() { | ||
return this.configuration.shells; | ||
} | ||
|
||
/** | ||
* Returns a shell command string specific for the current operating system, or a generic shell command if this shell | ||
* command does not have an explicit version for the current OS. | ||
*/ | ||
public getShellCommand(): string { | ||
let operating_system = getOperatingSystem(); | ||
|
||
// Check if the shell command has defined a specific command for this operating system. | ||
if (undefined === this.configuration.platform_specific_commands[operating_system]) { | ||
// No command is defined specifically for this operating system. | ||
// Return an "OS agnostic" command. | ||
return this.configuration.platform_specific_commands.default; | ||
} else { | ||
// The shell command has defined a specific command for this operating system. | ||
return this.configuration.platform_specific_commands[operating_system]; | ||
} | ||
} | ||
|
||
/** | ||
* Returns a version of the shell command that should be used if no platform specific command is defined for the | ||
* current platform. If you plan to use this for execution, consider using getShellCommand() instead, as it takes the | ||
* current platform into account. | ||
*/ | ||
public getDefaultShellCommand() { | ||
return this.configuration.platform_specific_commands.default; | ||
} | ||
|
||
public getPlatformSpecificShellCommands() { | ||
return this.configuration.platform_specific_commands; | ||
} | ||
|
||
public getAlias() { | ||
return this.configuration.alias; | ||
} | ||
|
||
public getConfirmExecution() { | ||
return this.configuration.confirm_execution; | ||
} | ||
|
||
public getIgnoreErrorCodes() { | ||
return this.configuration.ignore_error_codes; | ||
} | ||
|
||
public getOutputChannelOrder() { | ||
return this.configuration.output_channel_order; | ||
} | ||
|
||
public getOutputChannels() { | ||
return this.configuration.output_channels; | ||
} | ||
} |
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,32 @@ | ||
import {TShellCommand} from "./TShellCommand"; | ||
import ShellCommandsPlugin from "./main"; | ||
import {ShellCommandConfiguration} from "./settings/ShellCommandConfiguration"; | ||
import {cloneObject} from "./Common"; | ||
|
||
export class TShellCommandTemporary extends TShellCommand { | ||
|
||
/** | ||
* @private Do not create new objects directly, use fromTShellCommand() instead. | ||
* @param plugin | ||
* @param shell_command_configuration | ||
*/ | ||
constructor(plugin: ShellCommandsPlugin, shell_command_configuration: ShellCommandConfiguration) { | ||
super(plugin, null, shell_command_configuration); | ||
} | ||
|
||
public getId(): string { | ||
throw Error("TShellCommandTemporary does not have an ID, because it is a clone of a real TShellCommand that should not be saved."); | ||
} | ||
|
||
/** | ||
* Returns a TShellCommandTemporary instance, which contains configuration that is copied from the given TShellCommand. | ||
* The clone can be used for altering the configuration temporarily. The clone cannot be saved, and it's ID cannot be | ||
* accessed. | ||
*/ | ||
public static fromTShellCommand(t_shell_command: TShellCommand) { | ||
return new TShellCommandTemporary( | ||
t_shell_command.getPlugin(), | ||
cloneObject(t_shell_command.getConfiguration()), | ||
); | ||
} | ||
} |
Oops, something went wrong.