Skip to content

Commit

Permalink
Merge pull request #1 from foivospro/testing
Browse files Browse the repository at this point in the history
Complete refactoring of UBlocks
  • Loading branch information
simosathan9 authored Jan 5, 2025
2 parents 1738cee + 7a14989 commit a27f74d
Show file tree
Hide file tree
Showing 110 changed files with 2,281 additions and 2,976 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ npm-debug.log
.env
.history
bin/pre-commit
.idea
131 changes: 128 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,134 @@ To ensure that code is properly formatted before committing, set up the pre-comm

`chmod +x .git/hooks/pre-commit bin/pre-commit`

3. Format the code by running the following command:

`npm run prettier-fix`
# How to Create a New Block

Below are the instructions on how to create a new block in a separate file (e.g., `[blockName]Block.js`), and how to integrate it into the existing code generation system (`UnixGenerator`) that we have already implemented.

## 1. Create a New File for Your Block

Inside the folder where you keep your blocks (`blocks/`), create a new file named `[blockName]Block.js`.
For instance, if you want to create a block for a command called `foo`, you can name it `fooBlock.js`.

## 2. Define the Block Using a JSON Schema

In this new file, define your block using the JSON schema required by Blockly. Below is a basic structure:

```js
var fooBlock = {
type: 'foo', // Unique name for the block
category: 'My Custom Category', // Category in which the block will appear in the toolbox
unix_description: [
// The "unix_description" array defines how totranslate the block into a Unix command.
{
foo: 'customFoo',
printName: true, // "printName" specifies whether the block's type should appear explicitly in the generated command.
// Static Handling
myStaticField: '-s',
myDynamicField: (fieldValues) => {
// Example: Append the field value with a prefix
return `--option=${fieldValues['myDynamicField']}`;
}
}
],
message0: 'Message displayed on the block %1',
args0: [
{
type: 'field_input',
name: 'myStaticField',
text: 'default value'
}
],
message1: 'Message displayed on the block %1',
args1: [
{
type: 'field_input',
name: 'myDynamicField',
text: 'default value'
}
],
// Configuration for block connections (input, output, statement, etc.)
previousStatement: 'Action',
nextStatement: 'Action',
style: 'My Custom Category',
tooltip: 'Description of what this block does',
helpUrl: '' // Optional: link to further documentation
};

// Register the block with Blockly
Blockly.defineBlocksWithJsonArray([fooBlock]);

// Register the generator handler for this block
window.unixGenerator.forBlock['foo'] = window.unixGenerator.forBlock.generic;
```

### 2.1 Managing `unix_description`

In the **UnixGenerator** architecture, each **Blockly** block has an associated `unix_description`, which describes how its fields (`fieldValues`) and child blocks (`childCode`) should be translated into a Unix command. Also it is crucial that the keys used in unix_description correspond exactly to the names of the fields defined in args. This ensures that the generator can correctly map field values to their respective parts in the Unix command. Below is a high-level overview of how this works:

#### 2.1.1 **`unix_description` Structure**

- Typically, `unix_description` is an array of one or more objects. Each object specifies functions and/or flags that define how to compose the final command.
- Example:
```js
unix_description: [
{
printName: true, // Determines if the block's type should be printed in the command
someFieldName: '-f', // Static string to be added if the field is active
// Function to dynamically process fieldValues and childCode
someInputStatementName: (fieldValues, childCode) => {
// process fieldValues and childCode
return `-x ${childCode}`;
}
}
];
```

#### 2.1.2 **Using Functions vs. Static Strings**

- **Functions**: If you need to dynamically combine or manipulate `fieldValues` and `childCode`, you can define a function in `unix_description`:
```js
someFieldName: (fieldValues, childCode) => {
return `-o ${fieldValues['someParam']} ${childCode}`;
};
```
This function will be called by the generator, and whatever string it returns will be appended to the command.
- **Static Strings**: If you only need a fixed flag (e.g., `-r`, `--verbose`), you can simply specify a string:
```js
someFieldName: '-r';
```
The generator will automatically add `-r` when the corresponding field is “checked” or “selected,” depending on your field definition.

#### 2.1.3 **Choosing `generic` vs. `concat` Block Handlers**

- In your main generator setup, you decide how each block’s code is combined with subsequent blocks. You can choose:
```js
window.unixGenerator.forBlock['foo'] = window.unixGenerator.forBlock.generic;
```
or
```js
window.unixGenerator.forBlock['foo'] = window.unixGenerator.forBlock.concat;
```
- **`generic`**: This handler typically adds a pipe symbol (`|`) between commands, following the standard Unix “piping” convention.
- **`concat`**: This handler concatenates the output without a pipe or spaces, or uses a different connector if defined. It’s useful for scenarios where multiple arguments are strung together (e.g., building up a filename or a single argument line).

#### 2.1.4 Customizing Output with Block Names

**Specifying the Block's Name in `unix_description`:**
You can override the default block name in the generated command by specifying a different key in `unix_description`. For example:

```js
unix_description: [
{
foo: 'customFoo', // Instead of printing 'foo', it will print 'customFoo' in the command
printName: true
}
];
```

In this case, if the block's type is 'foo' and you set 'foo': 'customFoo' in unix_description, the generated command will include 'customFoo' instead of 'foo'. 3. Format the code by running the following command:

`npm run prettier-fix`

# Further information

Expand Down
Binary file modified db/blockly_unix_database.db
Binary file not shown.
24 changes: 13 additions & 11 deletions index.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!doctype html>
<html lang="en">
<html lang="en" translate="no">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
Expand All @@ -14,6 +14,7 @@
<script src="https://unpkg.com/@blockly/workspace-backpack@latest"></script>
<script src="https://unpkg.com/@blockly/plugin-cross-tab-copy-paste@latest"></script>
<script src="msg/en.js"></script>
<script src="js/block.js"></script>

<!-- <script src="https://unpkg.com/@blockly/block-plus-minus"></script> -->

Expand Down Expand Up @@ -111,14 +112,16 @@
<script src="blocks/arrayGetIndexBlock.js"></script>
<script src="blocks/arraySetIndexBlock.js"></script>
<script src="blocks/filenameVariableBlock.js"></script>
<script src="blocks/argumentCountBlock.js"></script>
<script src="blocks/argumentArrayBlock.js"></script>
<script src="blocks/environmentVariablesBlock.js"></script>
<script src="blocks/ARGCBlock.js"></script>
<script src="blocks/ARGVBlock.js"></script>
<script src="blocks/ENVIRONBlock.js"></script>
<script src="blocks/FNRBlock.js"></script>
<script src="blocks/FSBlock.js"></script>
<script src="blocks/OFSBlock.js"></script>
<script src="blocks/RLENGTHBlock.js"></script>
<script src="blocks/RSTARTBlock.js"></script>
<script src="blocks/builtinBlocks.js"></script>
<script src="blocks/mutators.js"></script>
<!-- END OF BLOCK DEFINITION -->
<script>
document.addEventListener('DOMContentLoaded', () => {
Expand Down Expand Up @@ -590,13 +593,13 @@
}
},
{ kind: 'block', type: 'atan2' },
{ kind: 'block', type: 'cos' },
{ kind: 'block', type: 'sin' },
{ kind: 'block', type: 'exp' },
{ kind: 'block', type: 'log' },
{ kind: 'block', type: 'cosine' },
{ kind: 'block', type: 'sine' },
{ kind: 'block', type: 'exponent' },
{ kind: 'block', type: 'logarithm' },
{ kind: 'block', type: 'sqrt' },
{ kind: 'block', type: 'rand' },
{ kind: 'block', type: 'seed' },
{ kind: 'block', type: 'random' },
{ kind: 'block', type: 'setSeed' },
{ kind: 'block', type: 'int' }
]
},
Expand Down Expand Up @@ -747,6 +750,5 @@
).weight = 3;
</script>
<script src="js/app.js"></script>
<script src="js/block.js"></script>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
var argumentCountBlock = {
var ARGCBlock = {
type: 'ARGC',
category: 'Field Processing',
unix_description: [
{
printName: true,
ARGC: 'ARGC'
}
],
Expand All @@ -16,11 +17,8 @@ var argumentCountBlock = {
style: 'Special Variables',
output: null,
tooltip: '%{BKY_ARGUMENT_COUNT_TOOLTIP}',
helpUrl: '%{BKY_ARGUMENT_COUNT_HELPURL}', // URL to further information or documentation.
generateCommand: function (block) {
var awkCommand = 'ARGC';
return awkCommand;
}
helpUrl: '%{BKY_ARGUMENT_COUNT_HELPURL}' // URL to further information or documentation.
};

Blockly.defineBlocksWithJsonArray([argumentCountBlock]);
Blockly.defineBlocksWithJsonArray([ARGCBlock]);
window.unixGenerator.forBlock['ARGC'] = window.unixGenerator.forBlock.concat;
27 changes: 27 additions & 0 deletions public/blocks/ARGVBlock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
var ARGVBlock = {
type: 'ARGV',
unix_description: [
{
printName: false,
INDEX: (childCode) => {
return 'ARGV[' + childCode + ']';
}
}
],
message0: '%{BKY_ARGUMENT_ARRAY}',
args0: [
{
type: 'input_value',
name: 'INDEX',
check: ['Number', 'String']
}
],
output: 'String',
style: 'Special Variables',
tooltip: '%{BKY_ARGUMENT_ARRAY_TOOLTIP}',
helpUrl: '%{BKY_ARGUMENT_ARRAY_HELPURL}'
};

Blockly.defineBlocksWithJsonArray([ARGVBlock]);

window.unixGenerator.forBlock['ARGV'] = window.unixGenerator.forBlock.concat;
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
var environBlock = {
var ENVIRONBlock = {
type: 'ENVIRON',
category: 'Field Processing',
unix_description: [
{
ENVIRON: 'ENVIRON'
printName: false,
HOME: 'ENVIRON["HOME"]',
PATH: 'ENVIRON["PATH"]',
USER: 'ENVIRON["USER"]',
SHELL: 'ENVIRON["SHELL"]',
PWD: 'ENVIRON["PWD"]',
LANG: 'ENVIRON["LANG"]',
TERM: 'ENVIRON["TERM"]',
EDITOR: 'ENVIRON["EDITOR"]',
MAIL: 'ENVIRON["MAIL"]',
LOGNAME: 'ENVIRON["LOGNAME"]',
HOSTNAME: 'ENVIRON["HOSTNAME"]',
OLDPWD: 'ENVIRON["OLDPWD"]',
LINES: 'ENVIRON["LINES"]',
COLUMNS: 'ENVIRON["COLUMNS"]'
}
],
message0: '%{BKY_ENVIRONMENT_VARIABLE} %1\n',
Expand Down Expand Up @@ -32,11 +46,9 @@ var environBlock = {
style: 'Special Variables',
output: null,
tooltip: '%{BKY_ENVIRONMENT_VARIABLE_TOOLTIP}',
helpUrl: '%{BKY_ENVIRONMENT_VARIABLE_HELPURL}', // URL to further information or documentation.
generateCommand: function (block) {
var envVar = block.getFieldValue('ENV_VAR');
return 'ENVIRON["' + envVar + '"]';
}
helpUrl: '%{BKY_ENVIRONMENT_VARIABLE_HELPURL}' // URL to further information or documentation.
};

Blockly.defineBlocksWithJsonArray([environBlock]);
Blockly.defineBlocksWithJsonArray([ENVIRONBlock]);
window.unixGenerator.forBlock['ENVIRON'] =
window.unixGenerator.forBlock.generic;
16 changes: 4 additions & 12 deletions public/blocks/FNRBlock.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,16 @@ var FNRBlock = {
category: 'Field Processing',
unix_description: [
{
printName: false,
FNR: 'FNR'
}
],
message0: '%{BKY_FNR} %1\n',
args0: [
{
type: 'input_dummy',
name: 'FieldNumber'
}
],
message0: '%{BKY_FNR}',
style: 'Special Variables',
output: null,
tooltip: '%{BKY_FNR_TOOLTIP}',
helpUrl: '%{BKY_FNR_HELPURL}', // URL to further information or documentation.
generateCommand: function (block) {
var awkCommand = 'FNR';
return awkCommand;
}
helpUrl: '%{BKY_FNR_HELPURL}' // URL to further information or documentation.
};

Blockly.defineBlocksWithJsonArray([FNRBlock]);
window.unixGenerator.forBlock['FNR'] = window.unixGenerator.forBlock.generic;
15 changes: 8 additions & 7 deletions public/blocks/FSBlock.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ var FSBlock = {
category: 'Field Processing',
unix_description: [
{
FieldSeparator: 'FS'
printName: false,
FieldSeparator: (fieldValues) => {
return 'FS = "' + fieldValues['FieldSeparator'] + '"; ';
}
}
],
message0: '%{BKY_FIELD_SEPARATOR}',
Expand All @@ -14,13 +17,11 @@ var FSBlock = {
}
],
style: 'Special Variables',
output: null,
previousStatement: null,
nextStatement: null,
tooltip: '%{BKY_FIELD_SEPARATOR_TOOLTIP}',
helpUrl: '%{BKY_FIELD_SEPARATOR_HELPURL}', // URL to further information or documentation.
generateCommand: function (block) {
var fieldSeparator = block.getFieldValue('FieldSeparator');
return 'FS = "' + fieldSeparator + '"';
}
helpUrl: '%{BKY_FIELD_SEPARATOR_HELPURL}' // URL to further information or documentation.
};

Blockly.defineBlocksWithJsonArray([FSBlock]);
window.unixGenerator.forBlock['FS'] = window.unixGenerator.forBlock.concat;
18 changes: 5 additions & 13 deletions public/blocks/NFBlock.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,16 @@ var NFBlock = {
category: 'Field Processing',
unix_description: [
{
FieldNumber: 'NF'
}
],
message0: '%{BKY_FIELD_NUMBER} %1\n',
args0: [
{
type: 'input_dummy',
name: 'FieldNumber'
printName: false,
NF: 'NF'
}
],
message0: '%{BKY_FIELD_NUMBER}',
style: 'Special Variables',
output: null,
tooltip: '%{BKY_FIELD_NUMBER_TOOLTIP}',
helpUrl: '%{BKY_FIELD_NUMBER_HELPURL}', // URL to further information or documentation.
generateCommand: function (block) {
var awkCommand = 'NF';
return awkCommand;
}
helpUrl: '%{BKY_FIELD_NUMBER_HELPURL}' // URL to further information or documentation.
};

Blockly.defineBlocksWithJsonArray([NFBlock]);
window.unixGenerator.forBlock['NF'] = window.unixGenerator.forBlock.generic;
Loading

0 comments on commit a27f74d

Please sign in to comment.