Skip to content

Commit

Permalink
Merge pull request #7 from butvinm-itmo/desktop
Browse files Browse the repository at this point in the history
Desktop application
  • Loading branch information
butvinm authored Sep 1, 2023
2 parents 6c96bc1 + 123d5f9 commit 3a2bcba
Show file tree
Hide file tree
Showing 47 changed files with 3,713 additions and 105 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

import cliapp.TextsManager;
import cliapp.cliclient.CLIClient;
import cliapp.cliclient.UserInputPipeline;
import cliapp.cliclient.exceptions.CommandNotFoundError;
import cliapp.cliclient.exceptions.InlineParamsError;
import commands.Command;
Expand Down Expand Up @@ -160,6 +159,61 @@ public <I, O> O askRequirement(Requirement<I, O> requirement) throws Requirement
}
}

/**
* Prepared pipeline.
*
* Contains static requirements and dynamic requirements,
* but if dynamic requirements are not enough, it asks them from source
* pipeline.
*/
private static class PreparedPipeline implements RequirementsPipeline {
private final RequirementsPipeline sourcePipeline;

private final Map<String, String> staticRequirementsMap;

private final List<String> dynamicRequirements;

private int askRequirementAttempts;

public PreparedPipeline(RequirementsPipeline sourcePipeline,
Map<String, String> staticRequirementsMap,
List<String> dynamicRequirements,
int askRequirementAttempts) {
this.sourcePipeline = sourcePipeline;
this.staticRequirementsMap = staticRequirementsMap;
this.dynamicRequirements = dynamicRequirements;
this.askRequirementAttempts = askRequirementAttempts;
}

/**
* Asks for the requirement and returns the value of it.
*
* @param requirement a {@link commands.requirements.Requirement} instance.
*
* @throws RequirementAskError if there's an error while asking the requirement.
*
* @return the value of the requirement.
*/
@Override
public <I, O> O askRequirement(Requirement<I, O> requirement) throws RequirementAskError {
try {
if (staticRequirementsMap.containsKey(requirement.getName())) {
// static requirements
return requirement.getValue((I) staticRequirementsMap.get(requirement.getName()));
} else {
// dynamic requirements
if (dynamicRequirements.isEmpty()) {
// if not enough dynamic requirements, ask them from source pipeline
return sourcePipeline.askRequirement(requirement);
}
return requirement.getValue((I) dynamicRequirements.remove(0));
}
} catch (ValidationError e) {
throw new RequirementAskError(requirement.getName(), e);
}
}
}

/**
* Extracts the list of static parameters from a given script line.
*
Expand Down Expand Up @@ -192,7 +246,8 @@ private List<String> extractDynamicParams(String line) {
* @param line the script line to execute with dynamic arguments
* @throws ExecutionError if an error occurs while executing the command
*/
private void executeWithDynamicParams(String line) throws ExecutionError {
private void executeWithDynamicParams(String line, RequirementsPipeline pipeline, OutputChannel output)
throws ExecutionError {
// parse line for static params
List<String> params = extractStaticParams(line);
String trigger = params.get(0);
Expand All @@ -214,11 +269,10 @@ private void executeWithDynamicParams(String line) throws ExecutionError {
// extract dynamic params
List<String> dynamicParams = extractDynamicParams(line);
// init output channel and pipeline
OutputChannel output = System.out::println;
RequirementsPipeline pipeline = new DirectLoadPipeline(staticRequirementsMap, dynamicParams);
RequirementsPipeline dlPipeline = new DirectLoadPipeline(staticRequirementsMap, dynamicParams);
// execute command
output.putString("Execute: " + command.getName() + " ...");
command.execute(pipeline, output);
command.execute(dlPipeline, output);
}

/**
Expand All @@ -227,7 +281,8 @@ private void executeWithDynamicParams(String line) throws ExecutionError {
* @param line the script line to execute with user input
* @throws ExecutionError if an error occurs while executing the command
*/
private void executeWithUserParams(String line) throws ExecutionError {
private void executeWithUserParams(String line, RequirementsPipeline pipeline, OutputChannel output)
throws ExecutionError {
// parse line for static params
List<String> params = extractStaticParams(line);
String trigger = params.get(0);
Expand All @@ -246,21 +301,18 @@ private void executeWithUserParams(String line) throws ExecutionError {
} catch (InlineParamsError e) {
throw new ExecutionError(e.getMessage());
}
// init output channel and pipeline
OutputChannel output = System.out::println;
RequirementsPipeline pipeline = new UserInputPipeline(
staticRequirementsMap,
client.getAskRequirementAttempts(),
client.getUserInputSupplier());

// execute command
RequirementsPipeline preparedPipeline = new PreparedPipeline(pipeline, staticRequirementsMap, List.of(), 3);
output.putString("Execute: " + command.getName() + " ...");
command.execute(pipeline, output);
command.execute(preparedPipeline, output);
}

/**
* Parse one script line, resolve params type and execute command
*/
private void executeScriptLine(String line) throws ExecutionError {
private void executeScriptLine(String line, RequirementsPipeline pipeline, OutputChannel output)
throws ExecutionError {
if (line.startsWith("#")) {
// comment
return;
Expand All @@ -269,10 +321,10 @@ private void executeScriptLine(String line) throws ExecutionError {
return;
} else if (line.matches("^.+\\{.+\\}$")) {
// with direct loaded params
executeWithDynamicParams(line);
executeWithDynamicParams(line, pipeline, output);
} else if (line.matches("^.+(\\{\\})?$")) {
// with params from user
executeWithUserParams(line);
executeWithUserParams(line, pipeline, output);
} else {
// without dynamic params
throw new ExecutionError(ts.t("ExecuteCommand.IncorrectLineFormat", line));
Expand All @@ -288,7 +340,8 @@ private void executeScriptLine(String line) throws ExecutionError {
* @throws ExecutionError If an error occurs while executing any of the script
* lines.
*/
private void executeScript(Path scriptPath, String script) throws ExecutionError {
private void executeScript(Path scriptPath, String script, RequirementsPipeline pipeline, OutputChannel output)
throws ExecutionError {
// update call counter
Integer callCount = callCounter.getOrDefault(scriptPath, 0);
callCounter.put(scriptPath, callCount + 1);
Expand All @@ -303,7 +356,7 @@ private void executeScript(Path scriptPath, String script) throws ExecutionError
// execute script
for (String line : script.split(System.lineSeparator())) {
try {
executeScriptLine(line);
executeScriptLine(line, pipeline, output);
} catch (Exception e) {
throw new ExecutionError(
ts.t("ExecuteCommand.LineError", line, e.getMessage()));
Expand Down Expand Up @@ -344,6 +397,6 @@ public void execute(RequirementsPipeline pipeline, OutputChannel output) throws
} catch (IOException e) {
throw new ExecutionError(e.getMessage(), e);
}
executeScript(scriptPath, scriptContent);
executeScript(scriptPath, scriptContent, pipeline, output);
}
}
93 changes: 93 additions & 0 deletions CollectionManager/desktop/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# File created using '.gitignore Generator' for Visual Studio Code: https://bit.ly/vscode-gig
# Created by https://www.toptal.com/developers/gitignore/api/windows,visualstudiocode,java,maven
# Edit at https://www.toptal.com/developers/gitignore?templates=windows,visualstudiocode,java,maven

### Java ###
# Compiled class file
*.class

# Log file
*.log

# BlueJ files
*.ctxt

# Mobile Tools for Java (J2ME)
.mtj.tmp/

# Package Files #
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
replay_pid*

### Maven ###
target/
pom.xml.tag
pom.xml.releaseBackup
pom.xml.versionsBackup
pom.xml.next
release.properties
dependency-reduced-pom.xml
buildNumber.properties
.mvn/timing.properties
# https://github.com/takari/maven-wrapper#usage-without-binary-jar
.mvn/wrapper/maven-wrapper.jar

# Eclipse m2e generated files
# Eclipse Core
.project
# JDT-specific (Eclipse Java Development Tools)
.classpath

### VisualStudioCode ###
.vscode/*

# Local History for Visual Studio Code
.history/

# Built Visual Studio Code Extensions
*.vsix

### VisualStudioCode Patch ###
# Ignore all local history of files
.history
.ionide

### Windows ###
# Windows thumbnail cache files
Thumbs.db
Thumbs.db:encryptable
ehthumbs.db
ehthumbs_vista.db

# Dump file
*.stackdump

# Folder config file
[Dd]esktop.ini

# Recycle Bin used on file shares
$RECYCLE.BIN/

# Windows Installer files
*.cab
*.msi
*.msix
*.msm
*.msp

# Windows shortcuts
*.lnk

# End of https://www.toptal.com/developers/gitignore/api/windows,visualstudiocode,java,maven

# Custom rules (everything added below won't be overriden by 'Generate .gitignore File' if you use 'Update' option)

Loading

0 comments on commit 3a2bcba

Please sign in to comment.