hernanmd / CodeGenerator

Collection of Pharo Smalltalk utilities related with code generation

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

license-badge PRs Welcome Project Status: Active – The project has reached a stable, usable state and is being actively developed.

Description

CodeGenerator package in Pharo Smalltalk

Installation

There are several ways to install the CodeGenerator package:

Core version

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.

Baseline String

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' ].
...

Code Generators Usage Examples

Smalltalk Generators

How to install:

EpMonitor disableDuring: [ 
	Metacello new	
	baseline: 'CodeGenerator';	
	repository: 'github://hernanmd/CodeGenerator/repository';	
	load: #('Smalltalk-Generators') ].

Simple class with getter and setter

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.

Class Hierarchy Generation

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

Singleton Design Pattern

CGStSingleton new
	setCleanTarget;
	targetClassCategory: 'SingletonEx1';
	targetClass: #SingletonEx1;
	generateMethods.
Smalltalk tools browser openOnClass: #SingletonEx1 asClass.

Visitor Design Pattern

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

SUnit Tests Generator.

CGSunit new
	setCleanTarget;
	inputClass: CGSmalltalk;
	targetTestClass: #TestCGSmalltalkExample;
	targetTestCategory: 'CGGeneratedCode';
	generateSetUp: true;
	generateTests.
Smalltalk tools browser openOnClass: #TestCGSmalltalkExample asClass.

Convenience Methods

CGStConvenienceMethods uniqueInstance
        setCleanTarget;
        parameterCount: 1;
        targetSelector: #cgConvEx1;
        targetClassCategory: 'ConvenienceMethodsEx';
        targetClass: #ConvenienceMethodsEx;
        generateMethods.

Finite State Machine

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.

Resource Importer

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.

Spec Generator

How to install:

Metacello new	
  baseline: 'CodeGenerator';	
  repository: 'github://hernanmd/CodeGenerator/repository';	
  load: #('SpecUI').

Usage example

CGStSpec new
	setCleanTarget;
	targetClass: #SpecModelClass1;
	title: 'Example Spec 1';
	generateMethods.
Smalltalk tools browser openOnClass: #SpecModelClass1 asClass.

NSIS Installer Script Generator

How to install:

Metacello new	
  baseline: 'CodeGenerator';	
  repository: 'github://hernanmd/CodeGenerator/repository';	
  load: #('NSIS').

Usage example

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.

ProjectFramework Generator

Requires: ProjectFramework

How to install:

Metacello new	
  baseline: 'CodeGenerator';	
  repository: 'github://hernanmd/CodeGenerator/repository';	
  load: #('ProjectFramework').

Usage example

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.

Magritte Generator

How to install:

Metacello new	
  baseline: 'CodeGenerator';	
  repository: 'github://hernanmd/CodeGenerator/repository';	
  load: #('ProjectFramework').

Usage examples

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.		

R Generator

How to install:

Metacello new	
  baseline: 'CodeGenerator';	
  repository: 'github://hernanmd/CodeGenerator/repository';	
  load: #('R').

Usage example

Work in progress

Bash Generator

How to install:

Metacello new	
  baseline: 'CodeGenerator';	
  repository: 'github://hernanmd/CodeGenerator/repository';	
  load: #('Bash').

Usage example

Work in progress

Contribute

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 git@github.com:YOUR_NAME/.git
  • Do some modifications
    • If you add some feature, create your feature branch: git checkout -b MY_NEW_FEATURE
  • 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!

License

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.

Authors

Hernán Morales Durand

About

Collection of Pharo Smalltalk utilities related with code generation

License:MIT License


Languages

Language:Smalltalk 99.3%Language:StringTemplate 0.7%