Skip to content

Commit

Permalink
Adds option to convert _path into tokens by default (#44)
Browse files Browse the repository at this point in the history
* adds option to convert _path into tokens by default

* Makes it compatible with MacOS and Windows

---------

Co-authored-by: Jaroslav <jar87876@adobe.com>
  • Loading branch information
jardicc and Jaroslav authored Sep 7, 2023
1 parent 4d971ce commit 10d30d0
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 24 deletions.
5 changes: 4 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{
"typescript.tsdk": "node_modules\\typescript\\lib"
"typescript.tsdk": "node_modules\\typescript\\lib",
"cSpell.words": [
"tokenify"
]
}
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 2.7.0

- adds option to convert `_path` into tokens by default in generated code

## 2.6.0

- adds icons to the recorded descriptors in descriptors list
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"manifestVersion": 5,
"name": "Alchemist",
"id": "2bcdb900",
"version": "2.6.0",
"version": "2.7.0",
"host": {
"app": "PS",
"minVersion": "24.2",
Expand Down
Binary file modified installer/2bcdb900_PS.ccx
Binary file not shown.
8 changes: 7 additions & 1 deletion src/inspector/components/Settings/SettingsContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ class Settings extends Component<TSettings, ISettingsState> {
const {settings: {makeRawDataEasyToInspect: ignoreRawData, maximumItems, fontSize, neverRecordActionNames, accordionExpandedIDs}, onSetRecordRaw, onSetFontSize, onNeverRecordActionNamesChanged} = this.props;
const {onSetGlobalOptions, settingsVisible: visible, setToggleSettings, onToggleAccordion} = this.props;
const {dialogOptions, modalBehavior, synchronousExecution, supportRawDataType} = this.props.descriptorSettings;
const {indent, singleQuotes, hideDontRecord, hideForceNotify, hide_isCommand,codeImports,codeWrappers} = this.props.globalSettings;
const {indent, singleQuotes, hideDontRecord, hideForceNotify, hide_isCommand,codeImports,codeWrappers,tokenify} = this.props.globalSettings;

const items: {val: TFontSizeSettings, label: string}[] = [
{label: "Tiny", val: "size-tiny"},
Expand Down Expand Up @@ -241,6 +241,12 @@ class Settings extends Component<TSettings, ISettingsState> {
checked={singleQuotes || undefined}
>Use single quotes</SP.Checkbox>
</div>
<div className="row">
<SP.Checkbox
onChange={(e) => onSetGlobalOptions({tokenify: !!e.target?.checked})}
checked={tokenify || undefined}
>Convert _path to tokens</SP.Checkbox>
</div>

<div className="row">
<SP.Checkbox
Expand Down
4 changes: 3 additions & 1 deletion src/inspector/inspInitialState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { getSorInitialState } from "../sorcerer/sorInitialState";

export function getInitialState(): IInspectorState {
return {
version: [12, 0, 0],
version: [13, 0, 0],
selectedReferenceType: "layer",
filterBySelectedReferenceType: "off",
descriptorsGrouping: "eventName",
Expand Down Expand Up @@ -183,6 +183,7 @@ export function getInitialState(): IInspectorState {
},
},
settings: {

settingsVisible:false,
/** Sometimes you can get data when object in reference array is selected. This option is intended to select that item automatically for you */
fontSize: "size-default",
Expand Down Expand Up @@ -700,6 +701,7 @@ export function getInitialState(): IInspectorState {
singleQuotes: false,
codeImports: "require",
codeWrappers: "modal",
tokenify: true,

hide_isCommand: true,
hideDontRecord: true,
Expand Down
1 change: 1 addition & 0 deletions src/inspector/model/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ export interface ISettings {
neverRecordActionNames: string[]
accordionExpandedIDs: string[]

tokenify:boolean
singleQuotes: boolean
indent: "tab" | "space1" | "space2" | "space3" | "space4" | "space5" | "space6" | "space7" | "space8"

Expand Down
60 changes: 42 additions & 18 deletions src/inspector/selectors/inspectorCodeSelectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ export const getGeneratedCode = createSelector([
getReplayEnabled, getDescriptorOptions, getInspectorSettings, getIndentString,
], (selected, autoActive, treePath, replayEnabled, descOptions, settings, tab) => {

let shouldShowTokenify = false;

function makeNicePropertyPath(segments: string[]): string {
const regex = /^[a-zA-Z_$][0-9a-zA-Z_$]*$/m;

Expand Down Expand Up @@ -93,27 +95,27 @@ export const getGeneratedCode = createSelector([

const res: BatchPlayCommandOptionsExtended = {};

if (hasAnySync) { res.synchronousExecution = true; }
else if (hasAnyAsync) { res.synchronousExecution = false; }
if (hasAnySync) {res.synchronousExecution = true;}
else if (hasAnyAsync) {res.synchronousExecution = false;}

if (modalIsExecute) { res.modalBehavior = "execute"; }
else if (modalIsWait) { res.modalBehavior = "wait"; }
else if (!modalIsDefault) { res.modalBehavior = "fail"; }
if (modalIsExecute) {res.modalBehavior = "execute";}
else if (modalIsWait) {res.modalBehavior = "wait";}
else if (!modalIsDefault) {res.modalBehavior = "fail";}

return res;
}

// adds indentation
function idt(str:string):string {
function idt(str: string): string {
return str.split("\n").map(l => tab + l).join("\n");
}

function udt(str: string): string{
function udt(str: string): string {
return str.split("\n").map(l => l.replace(tab, "")).join("\n");
}

// replaces quotes
function qts(str:string):string {
function qts(str: string): string {
if (settings.singleQuotes) {
str = str.replaceAll(`"`, `'`);
}
Expand All @@ -124,13 +126,23 @@ export const getGeneratedCode = createSelector([
const wrappers = settings.codeWrappers;

if (selected.length >= 1 || autoActive) {
let data:any = null, iDesc: IDescriptor[] = [];
let data: any = null, iDesc: IDescriptor[] = [];

const stringifyOptions = {
singleQuotes: settings.singleQuotes,
indent: tab,
transform: (input: any | object, prop: number | string | symbol, originalResult: string) => {
if (settings.tokenify && prop === "_path") {
shouldShowTokenify = true;
console.log(input, originalResult);
const res = qts(`await tokenify("${input._path.replaceAll("\\","/")}")`);
return res;
}
return originalResult;
},
};

const tokenifyInfo = "// Please make sure that file system access permission in manifest.json has correct value.\n\n";
const playAbleInfo = "// Alchemist can't generate code from the reply of replay and from dispatched code.";
const notifierWarning = "// Events recognized as notifiers are not re-playable in most of the cases. There is high chance that generated code won't work.\n\n";

Expand All @@ -139,7 +151,7 @@ export const getGeneratedCode = createSelector([
} else if (autoActive) {
iDesc = [autoActive];
}
if (iDesc.some(item => ["replies","dispatcher"].includes(item.originalReference.type))) {
if (iDesc.some(item => ["replies", "dispatcher"].includes(item.originalReference.type))) {
return playAbleInfo;
}
data = iDesc.map(item => addPerItemOptions(item));
Expand All @@ -149,20 +161,20 @@ export const getGeneratedCode = createSelector([
if (Array.isArray(data)) {
data.forEach(d => {
if (settings.hideDontRecord) {
delete d.dontRecord;
delete d.dontRecord;
}
if (settings.hideForceNotify) {
delete d.forceNotify;
delete d.forceNotify;
}
if (settings.hide_isCommand) {
delete d._isCommand;
}
}
});
}

for (let i = 0; i < data.length; i++) {
const item = data[i];
RawDataConverter.convertFakeRawInCode(item,descOptions);
RawDataConverter.convertFakeRawInCode(item, descOptions);
}

let strPinned = "";
Expand All @@ -175,34 +187,46 @@ export const getGeneratedCode = createSelector([
const commandOptions = addCommonOptions(iDesc);

const strOptions = idt(stringifyObject(commandOptions, stringifyOptions));
const strDesc:string = stringifyObject(data, stringifyOptions);
const strDesc: string = stringifyObject(data, stringifyOptions);


const strExecModalImport = addModules ? qts(`const {executeAsModal} = require("photoshop").core;\n`) : "";
const strBatchPlayImport = addModules ? qts(`const {batchPlay} = require("photoshop").action;\n\n`) : "";
const strBatchPlayImport = addModules ? qts(`const {batchPlay} = require("photoshop").action;\n`) : "";
const strTokenifyImport = addModules && shouldShowTokenify ? qts(`const {localFileSystem: fs} = require("uxp").storage;\n`) : "";
const strNewLine = addModules ? "\n" : "";

const strBatchPlay = `const result = await batchPlay(\n${idt(strDesc)},\n${strOptions}\n);${strPinned}`;
const strActionCommand = `async function actionCommands() {\n${idt(strBatchPlay)}\n}\n\n`;

const strCall = qts(`async function runModalFunction() {\n${tab}await executeAsModal(actionCommands, {"commandName": "Action Commands"});\n}\n\nawait runModalFunction();\n`);
const strTokenify = shouldShowTokenify ? qts(`async function tokenify(url){\n${tab}return fs.createSessionToken(await fs.getEntryWithUrl("file:" + url));\n}\n\n`) : "";

let banner = "";
if (iDesc.some(item => item.originalReference.type === "notifier")) {
banner = notifierWarning;
}
if (shouldShowTokenify) {
banner += tokenifyInfo;
}


if (wrappers==="batchPlay") {
if (wrappers === "batchPlay") {
return (
banner +
strBatchPlayImport +
strTokenifyImport +
strNewLine +
strTokenify +
strBatchPlay
);
} else if (wrappers==="modal") {
} else if (wrappers === "modal") {
return (
banner +
strExecModalImport +
strBatchPlayImport +
strTokenifyImport +
strNewLine +
strTokenify +
strActionCommand +
strCall
);
Expand Down
2 changes: 1 addition & 1 deletion uxp/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"manifestVersion": 5,
"name": "Alchemist",
"id": "2bcdb900",
"version": "2.6.0",
"version": "2.7.0",
"host": {
"app": "PS",
"minVersion": "24.2",
Expand Down

0 comments on commit 10d30d0

Please sign in to comment.