CodeGenerator package in Pharo Smalltalk
There are several ways to install the CodeGenerator package:
The Core version includes only the basic abstract classes which provide core features: A Templates protocol and Authoring holder.
EpMonitor disableDuring: [
Metacello new
baseline: 'CodeGenerator';
repository: 'github://hernanmd/CodeGenerator/repository';
load ]
Each of the following generators has installer expressions described in their own section.
If you are building a BaselineOf, copy and paste the following code to make use of CodeGenerator.
...
spec
baseline: 'CodeGenerator'
with: [ spec repository: 'github://hernanmd/CodeGenerator/repository' ].
...
How to install:
EpMonitor disableDuring: [
Metacello new
baseline: 'CodeGenerator';
repository: 'github://hernanmd/CodeGenerator/repository';
load: #('Smalltalk-Generators') ].
The following generator will create a new class in the image, adding two instance variables and creating their accessors (getters and setters):
CGSmalltalk new
setCleanTarget;
targetClassCategory: 'MPIRunner-Core';
targetClass: #MPIRunner;
addIVarsWithAccessors: #(#nproc #submitToQueue).
Smalltalk tools browser openOnClass: #MPIRunner asClass.
Generation of Class Hierarchy with three concrete subclasses, specifying a prefix for method names:
CGStAbstractClassPattern new
setCleanTarget;
targetClassCategory: 'CGGeneratedCode-Core';
targetClass: #CGExampleAbstractClass2;
concreteClassesCount: 3;
concreteClassNamePattern: #CGExampleConcreteClass2;
generateClasses;
operationsCount: 3;
operationsName: #exampleOperation;
generateOperations.
Smalltalk tools browser openOnClass: #CGExampleAbstractClass2 asClass.
The resulting hierarchy is:
CGExampleAbstractClass2
CGExampleConcreteClass21
CGExampleConcreteClass22
CGExampleConcreteClass23
CGStSingleton new
setCleanTarget;
targetClassCategory: 'SingletonEx1';
targetClass: #SingletonEx1;
generateMethods.
Smalltalk tools browser openOnClass: #SingletonEx1 asClass.
CGStBuilderPattern uniqueInstance
setCleanTarget;
targetClassCategory: 'CGGeneratedCode-Core';
" Abstract Builder class name (ex: UIBuilder) "
targetClass: #CGExCarBuilder;
productNames: #('CGExFordCar' 'CGExBMWCar' 'CGExHondaCar');
builderNames: #('CGExFordBuilder' 'CGExBMWBuilder' 'CGExHondaBuilder');
familyNames: #('CGExCarEngine' 'CGExCarBody');
" How many product parts per family ? "
productPartsPerFamilyCount: 3;
directorClassName: #CGExCarAssembler;
generateClasses.
Smalltalk tools browser openOnClass: #CGExCarBuilder asClass.
SUnit Tests Generator.
CGSunit new
setCleanTarget;
inputClass: CGSmalltalk;
targetTestClass: #TestCGSmalltalkExample;
targetTestCategory: 'CGGeneratedCode';
generateSetUp: true;
generateTests.
Smalltalk tools browser openOnClass: #TestCGSmalltalkExample asClass.
CGStConvenienceMethods uniqueInstance
setCleanTarget;
parameterCount: 1;
targetSelector: #cgConvEx1;
targetClassCategory: 'ConvenienceMethodsEx';
targetClass: #ConvenienceMethodsEx;
generateMethods.
CGStFSM_CaseStmts1 new
setCleanTarget;
targetClassCategory: 'FSM-Core';
targetClass: #FSMCaseStmts1;
targetSelector: #someMessage;
initialState: #state0;
transitions: (Dictionary new
at: #state0 put: #state1;
at: #state1 put: #state0;
yourself);
generateMethods.
Smalltalk tools browser openOnClass: #FSMCaseStmts1 asClass.
This generator open a directory selector dialog. The selected directory is used as source to scan image files and import them as Smalltalk methods. Currently supported file types are: 'jpg' 'jpeg' 'png' 'gif' 'ico' 'bmp'. For each image file, the name of the file concatenated with its extension (uppercase first extension letter) is used as method name.
CGStImageImporter new
setCleanTarget;
targetClass: #CGExampleResources1;
targetClassCategory: 'CGGeneratedCode';
import.
Smalltalk tools browser openOnClass: #CGExampleResources1 asClass.
This generator open a directory selector dialog. The selected directory is used as source to scan text files and import them as Smalltalk methods. Currently supported file types are: 'txt' 'csv'. For each image file, the name of the file concatenated with its extension (uppercase first extension letter) is used as method name.
CGStTextImporter new
setCleanTarget;
targetClass: #CGExampleResources2;
targetClassCategory: 'CGGeneratedCode';
import.
Smalltalk tools browser openOnClass: #CGExampleResources2 asClass.
Metacello new
baseline: 'CodeGenerator';
repository: 'github://hernanmd/CodeGenerator/repository';
load: #('SpecUI').
CGStSpec new
setCleanTarget;
targetClass: #SpecModelClass1;
title: 'Example Spec 1';
generateMethods.
Smalltalk tools browser openOnClass: #SpecModelClass1 asClass.
How to install:
Metacello new
baseline: 'CodeGenerator';
repository: 'github://hernanmd/CodeGenerator/repository';
load: #('NSIS').
CGNSISIPharo5AppInstaller new
product: 'MyProduct';
identity: 'MyName';
version: '1.0.0';
url: 'http://www.google.com';
launcher: 'MyLauncher.exe';
licenseEnFileName: 'LICENSE_ENGLISH';
iconFile: 'MyProduct.ico';
welcomeBmpFile: 'MyProduct.bmp';
splashBMPFileName: 'Splash.bmp';
generate.
Requires: ProjectFramework
Metacello new
baseline: 'CodeGenerator';
repository: 'github://hernanmd/CodeGenerator/repository';
load: #('ProjectFramework').
CGStProjectFramework new
setCleanTarget;
targetClassCategory: 'PFExampleCategory';
targetSuperclass: #PFStandardProjectWindow;
targetClass: #PFExampleWindow;
applicationClassName: #PFExampleApplicationClass;
projectClassName: #PFExampleProjectClass;
title: 'Example Project UI';
closeAfterCreateProjectSetting: true;
generateMethods.
Smalltalk tools browser openOnClass: #PFExampleWindow asClass.
Metacello new
baseline: 'CodeGenerator';
repository: 'github://hernanmd/CodeGenerator/repository';
load: #('ProjectFramework').
Example setting priorities:
CGStMagritte new
inputClass: ModelClass1;
targetClass: ModelClass1;
setBooleanAttributes: 'instVar1' label: 'label iVar1' priority: 10;
setDateAndTimeAttributes: 'instVar2' label: 'label Date and Time' priority: 20;
setDateAttributes: 'instVar3' label: 'label Date' priority: 30;
setFileAttributes: 'iVarFile' label: 'label File' priority: 40;
setMemoAttributes: 'iVarMemo' label: 'label Memo' priority: 50;
setMultiOptionAttributes: 'iVarMultiOption' label: 'label Multi Option' priority: 60;
setNumberAttributes: 'iVarInteger' label: 'label Integer' priority: 70.
Smalltalk tools browser openOnClass: ModelClass1.
Example setting defaults:
CGStMagritte new
inputClass: ModelClass1;
targetClass: ModelClass1;
setBooleanAttributes: 'instVar1' default: true label: 'label iVar1' priority: 10;
setDateAndTimeAttributes: 'instVar2' default: DateAndTime today label: 'label Date and Time' priority: 20;
setDateAttributes: 'instVar3' default: Date today label: 'label Date' priority: 30;
setFileAttributes: 'iVarFile' label: 'label File' priority: 40;
setMemoAttributes: 'iVarMemo' default: 'Memo' label: 'label Memo' priority: 50;
setMultiOptionAttributes: 'iVarMulti' default: #(One Two) label: 'label Multi Option' priority: 60;
setNumberAttributes: 'iVarInteger' default: 10 label: 'label Integer' priority: 70.
Smalltalk tools browser openOnClass: ModelClass1.
Metacello new
baseline: 'CodeGenerator';
repository: 'github://hernanmd/CodeGenerator/repository';
load: #('R').
Work in progress
Metacello new
baseline: 'CodeGenerator';
repository: 'github://hernanmd/CodeGenerator/repository';
load: #('Bash').
Work in progress
Working on your first Pull Request? You can learn how from this free series How to Contribute to an Open Source Project on GitHub
If you have discovered a bug or have a feature suggestion, feel free to create an issue on Github. If you have any suggestions for how this package could be improved, please get in touch or suggest an improvement using the GitHub issues page. If you'd like to make some changes yourself, see the following:
- Fork this repository to your own GitHub account using the web interface.
- Clone it to your local device:
git clone [email protected]:YOUR_NAME/.git
- Do some modifications
- If you add some feature, create your feature branch:
git checkout -b MY_NEW_FEATURE
- If you add some feature, create your feature branch:
- Test.
- Add to add yourself as author below.
- Finally, submit a pull request with your changes!
- This project follows the all-contributors specification. Contributions of any kind are welcome!
This software is licensed under the MIT License.
Copyright Hernán Morales Durand, 2018-2021.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Hernán Morales Durand