Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

User natives #206

Open
wants to merge 24 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
"name": "Run Wollok CLI tests",
"request": "launch",
"type": "node-terminal"
},
{
"command": "npm run test:file ${file}",
"name": "Run a single test file",
"request": "launch",
"type": "node-terminal"
}
]
}
4 changes: 4 additions & 0 deletions examples/init-examples/existing-folder/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@
.history

# Wollok Log
log
*.log

# Dependencies
node_modules
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const packageModel = {
*nativeTwo(self) {
return yield* this.reify(2)
},
}

module.exports = { packageModel };
7 changes: 7 additions & 0 deletions examples/user-natives/myNativesFolder/rootFile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const myModel = {
*nativeOne(this: any, _self: any): any {
return yield* this.reify(1)
},
}

export { myModel }
3 changes: 3 additions & 0 deletions examples/user-natives/myPackage/myInnerFile.wlk
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
object packageModel {
method nativeTwo() native
}
8 changes: 8 additions & 0 deletions examples/user-natives/myProgram.wpgm
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import myPackage.myInnerFile.packageModel
import rootFile.myModel

program myProgram {

console.println(myModel.nativeOne())
console.println(packageModel.nativeTwo())
}
9 changes: 9 additions & 0 deletions examples/user-natives/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "user-natives",
"version": "1.0.0",
"resourceFolder": "assets",
"wollokVersion": "4.0.0",
"author": "leo",
"license": "ISC",
"natives": "myNativesFolder"
}
3 changes: 3 additions & 0 deletions examples/user-natives/rootFile.wlk
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
object myModel {
method nativeOne() native
}
7 changes: 7 additions & 0 deletions examples/user-natives/userNatives.wtest
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import rootFile.myModel
import myPackage.myInnerFile.packageModel

test "call a native method used by user" {
assert.equals(1, myModel.nativeOne())
assert.equals(2, packageModel.nativeTwo())
}
125 changes: 42 additions & 83 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"lint": "eslint . ",
"lint:fix": "eslint . --fix",
"test:unit": "mocha --parallel -r ts-node/register/transpile-only test/**/*.test.ts --timeout 5000",
"test:file": "mocha --parallel -r ts-node/register/transpile-only",
"build:tools": "shx cp ./node_modules/wollok-web-tools/dist/web/game-index.js ./public/game/lib && shx cp ./node_modules/wollok-web-tools/dist/dynamicDiagram/diagram-index.js ./public/diagram",
"build": "shx rm -rf build && shx mkdir ./build && shx cp -r ./public ./build/public && tsc -p ./tsconfig.build.json",
"watch": "npm run build -- -w",
Expand Down Expand Up @@ -67,7 +68,8 @@
"socket.io": "^4.5.1",
"winston": "^3.11.0",
"wollok-ts": "^4.1.9",
"wollok-web-tools": "^1.1.7"
"wollok-web-tools": "^1.1.7",
"typescript": "~5.5.1"
},
"devDependencies": {
"@stylistic/eslint-plugin-ts": "^2.8.0",
Expand All @@ -91,7 +93,6 @@
"shx": "^0.3.4",
"sinon": "^17.0.1",
"socket.io-client": "^4.7.5",
"ts-node": "10.9.1",
"typescript": "~5.5.1"
"ts-node": "10.9.1"
}
}
28 changes: 28 additions & 0 deletions src/commands/dependencies.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { execSync } from 'child_process'
import path from 'path'
import logger from 'loglevel'

export type Options = {
project: string
verbose: boolean
}

const executeNpmCommand = (command: string, project: string, verbose: boolean): void => {
const fullCommand = `npm ${command}`
if (verbose) {
logger.info(`Executing in ${project}: ${fullCommand}`)
}
execSync(fullCommand, { cwd: path.resolve(project), stdio: 'inherit' })
}

export const addDependency = (pkg: string, { project, verbose }: Options): void => {
executeNpmCommand(`install ${pkg}`, project, verbose)
}

export const removeDependency = (pkg: string, { project, verbose }: Options): void => {
executeNpmCommand(`uninstall ${pkg}`, project, verbose)
}
Comment on lines +18 to +24
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A estos no hay que mandarles -s para que queden guardados en el package.json?


export const synchronizeDependencies = ({ project, verbose }: Options): void => {
executeNpmCommand('install', project, verbose)
}
19 changes: 13 additions & 6 deletions src/commands/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ export type Options = {
noTest: boolean,
noCI: boolean,
game: boolean,
noGit: boolean
noGit: boolean,
natives?: string
}

export default function (folder: string | undefined, { project: _project, name, noTest = false, noCI = false, game = false, noGit = false }: Options): void {
export default function (folder: string | undefined, { project: _project, name, noTest = false, noCI = false, game = false, noGit = false, natives = undefined }: Options): void {
const project = join(_project, folder ?? '')
const nativesFolder = join(project, natives ?? '')
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

si me pasaron natives, entonces es esa carpeta, si no es la misma del project


// Initialization
if (existsSync(join(project, 'package.json'))) {
Expand All @@ -28,6 +30,7 @@ export default function (folder: string | undefined, { project: _project, name,

// Creating folders
createFolderIfNotExists(project)
createFolderIfNotExists(nativesFolder)
createFolderIfNotExists(join(project, '.github'))
createFolderIfNotExists(join(project, '.github', 'workflows'))
if (game) {
Expand All @@ -52,7 +55,7 @@ export default function (folder: string | undefined, { project: _project, name,
}

logger.info('Creating package.json')
writeFileSync(join(project, 'package.json'), packageJsonDefinition(project, game))
writeFileSync(join(project, 'package.json'), packageJsonDefinition(project, game, natives ))

if (!noCI) {
logger.info('Creating CI files')
Expand Down Expand Up @@ -130,16 +133,16 @@ program PepitaGame {
}
`

const packageJsonDefinition = (projectName: string, game: boolean) => `{
const packageJsonDefinition = (projectName: string, game: boolean, natives?: string) => `{
"name": "${basename(projectName)}",
"version": "1.0.0",
${game ? assetsConfiguration() : ''}"wollokVersion": "4.0.0",
"author": "${userInfo().username}",
"author": "${userInfo().username}",${nativesConfiguration(natives)}
"license": "ISC"
}
`

const assetsConfiguration = () => `"resourceFolder": "assets",${ENTER} `
const nativesConfiguration = (natives?: string) => natives ? `${ENTER} "natives": "${natives}",` : ''

const ymlForCI = `name: build

Expand Down Expand Up @@ -170,5 +173,9 @@ const gitignore = `
.history

# Wollok Log
log
*.log

# Dependencies
node_modules
`
Loading
Loading