Skip to content

Commit

Permalink
Refactor & Modernize (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
coilysiren authored Oct 14, 2023
1 parent eb54580 commit 31d74c8
Show file tree
Hide file tree
Showing 21 changed files with 449 additions and 437 deletions.
9 changes: 2 additions & 7 deletions .github/workflows/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,5 @@ jobs:
- name: checkout code
uses: actions/checkout@v3

- name: install prereqs
run: |
pip install pipenv
pipenv sync
- name: run tests
run: pipenv run tests ${{ matrix.language }} any
- run: pip install invoke pyyaml
- run: invoke test ${{ matrix.language }} any
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
.python-version
.vscode
data/output*
data/sort_by_*
data/sorted_by_*
src/rust/target/
__pycache__/
57 changes: 45 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,61 @@

pronounced like "auto zone", a place for figuring out algorithms

## Installation requirements
## Installation Prereqs

- python
- pipenv
- docker

## Usage

Run this once
Install the required dependencies:

```bash
pipenv sync
pip install invoke pyyaml
```

Then run any of these
Then run any of the algos:

```bash
# example CLI calls, run with as many args so you want
pipenv run tests
pipenv run tests $language $script
pipenv run tests python
pipenv run tests python insertion_sort
# "any" is a wildcard keyword
pipenv run tests python any
invoke test $language $script
invoke test python insertion_sort
invoke test python any # "any" is a wildcard keyword
invoke test rust selection_sort
```

You will get output like so:

```bash
$ invoke test python any

> 🟢 script "./src/python/sort_builtin.py" succeeded
> 🟢 script "./src/python/sort_bubble_sort.py" succeeded
> 🟢 script "./src/python/sort_selection_sort.py" succeeded
> 🟢 script "./src/python/sort_insertion_sort.py" succeeded
> ✨ script run success ✨
```

_(note: I will likely not bother to update the above example when the output changes in minor ways)_

## New Languages

Adding new languages is a multi-step process, wherein you have to adapt each language to use parent python script's API. Which is to say, `tasks.py` expects every language to operate in roughly the same way, so each language needs modification to give them some uniformity of behavior. algo-zone was implemented in python first, so expect languages to be easier to add the more similar they are to python.

The broad steps to adding a language, are:

- Add it to `config.yml`. The only truly "required" keys are the `dockerImage` and `scriptInvoker`. Everything else is about modifying the languages to behave in a uniform manner. Your new language will likely require its own new special one-off key, like rust needing `useShortScriptName`.
- Create a folder in `src/`. If you're lucky, then you can get away with `src/` only needing the source file. The first source file you will want to add is `sort_builtin`. Every `sort_builtin` file just calls the language's native sorting function. You will also of course want to add any necessary language metadata, like `go.mod` or `Cargo.toml`.
- Handle any special cases. There are always special cases. I can only imagine this repo running out of special cases once it has 10+ languages. Some existing special cases include:

- Languages that simply do not have a way to say "run this one individual language file inside this folder". `golang` has this. The workaround was to make every file be a "test" file.
- Compiled languages each require a special something. `rust` specifically requires the target scripts to be mentioned in its manifest file (`Cargo.toml`). This can result in confusion if you add a new algo but forget to add it to the manifest file.

The most important thing to understand is: you need to figure out how to get your language to execute some code in a specific file. It its weird and complicated, when you add your new special case handling code to `tasks.py`. After that, you should be able to do:

```bash
invoke test cobol insertion_sort
```

Which will spin up a docker container for your brand new fancy language. The tests run inside that docker container. That docker container will be expecting a file as output, look at the existing language examples to get an idea of what this means.

Overall you should expect this process to take a few hours. It's hard getting all these languages to play nice with each other!!!
18 changes: 17 additions & 1 deletion config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,30 @@
python:
dockerImage: python:3.11
scriptInvoker: python
ignoreFiles:
- __init__.py
- helpers.py

# https://releases.rs/
# https://hub.docker.com/_/rust
rust:
dockerImage: rust:1.72
dockerImage: rust:1.73
scriptInvoker: cargo run --manifest-path ./src/rust/Cargo.toml --bin
envVars: RUST_BACKTRACE=1
useShortScriptName: true
ignoreFiles:
- Cargo.lock
- Cargo.toml

# https://endoflife.date/go
# https://hub.docker.com/_/golang
go:
dockerImage: golang:1.21
scriptInvoker: go test
scriptSuffix: ./src/go/helpers.go
ignoreFiles:
- go.mod
- helpers.go

# https://endoflife.date/ruby
# https://hub.docker.com/_/ruby
Expand All @@ -30,3 +39,10 @@ ruby:
js:
dockerImage: node:20
scriptInvoker: node

# TODO: not yet implemented
# https://endoflife.date/oracle-jdk
# https://hub.docker.com/_/amazoncorretto
java:
dockerImage: amazoncorretto:21
scriptInvoker: java bla bla bla
File renamed without changes.
File renamed without changes.
3 changes: 0 additions & 3 deletions scripts/readme.md

This file was deleted.

173 changes: 0 additions & 173 deletions scripts/tests.py

This file was deleted.

18 changes: 9 additions & 9 deletions src/go/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@ package algozone

import (
"fmt"
"io/ioutil"
"path"
"os"
"path/filepath"
"strings"
)

type sortFunc func([]string) []string

const randomizedDataPath = "./../../data/randomized.txt"
const sortedDataPath = "./../../data/sorted.txt"
var inputPath = os.Getenv("INPUT_PATH")
var outputPath = os.Getenv("OUTPUT_PATH")

func getInputList() (inputList []string, err error) {
// read input file
fileBytes, err := ioutil.ReadFile(randomizedDataPath)
fileBytes, err := os.ReadFile(filepath.Join("..", "..", inputPath))
if err != nil {
err = fmt.Errorf("error reading input file path: %w", err)
return nil, err
Expand All @@ -23,15 +23,15 @@ func getInputList() (inputList []string, err error) {

// clean input data
if fileSlice[len(fileSlice)-1] == "" {
fileSlice = fileSlice[:len(fileSlice)]
fileSlice = fileSlice[:len(fileSlice)-1]
}

return fileSlice, nil
}

func writeAndCompareOutputList(outputList []string, filename string) (err error) {
func writeAndCompareOutputList(outputList []string) (err error) {
// setup
filePath := path.Join(sortedDataPath, "..", filename+".txt")
filePath := filepath.Join("..", "..", outputPath)

// clean data
outputString := strings.Join(outputList, "\n")
Expand All @@ -43,7 +43,7 @@ func writeAndCompareOutputList(outputList []string, filename string) (err error)
outputString += "\n"
outputBytes := []byte(outputString)
// write file finally
err = ioutil.WriteFile(filePath, outputBytes, 0644)
err = os.WriteFile(filePath, outputBytes, 0644)
if err != nil {
err = fmt.Errorf("error writing output: %w", err)
return err
Expand Down
Loading

0 comments on commit 31d74c8

Please sign in to comment.