Skip to content

Commit

Permalink
Cli rendering (#170)
Browse files Browse the repository at this point in the history
* WIP with dummy data

* Vite/Node loading static files

* CLI with dummy data

* Project json parsing

* Github action test

* Update Tests

* Wait for curl

* Update readme

Co-authored-by: mmcardle <[email protected]>
  • Loading branch information
mmcardle and mmcardle-axial3d authored Nov 14, 2022
1 parent 2aa2418 commit 52f36e0
Show file tree
Hide file tree
Showing 15 changed files with 797 additions and 445 deletions.
9 changes: 9 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ jobs:
- run: yarn
- run: yarn test:unit

cli_tests:
name: CLI Tests
needs: [build]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- run: yarn
- run: script/run.sh

e2e_tests:
name: E2E Tests
needs: [build]
Expand Down
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,6 @@ coverage/
# Build if changed
.bic_cache

**/.venv
**/.venv
DjangoProject/
output.tar
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,28 @@ http://mmcardle.github.io/django_builder/
yarn install
```

### CLI interface

You can create and run a basic Django project from the command line

See [example-project.json](example-project.json)

```
./bin/django-builder example-project.json DjangoProject.tar
tar -xvf DjangoProject.tar
cd DjangoProject
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
python manage.py makemigrations
python manage.py migrate
python manage.py runserver
```

Head to http://127.0.0.1:8000

#### Firebase
```
npm install -g firebase-tools
Expand Down
4 changes: 4 additions & 0 deletions bin/django-builder
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env node

require = require('esm')(module /*, options*/);
require('../src/cli').cli(process.argv);
35 changes: 35 additions & 0 deletions example-project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"name": "DjangoProject",
"description": "A Django Project with great potential",
"htmx": false,
"apps": [
{
"name": "DjangoApp1",
"models": [
{
"name":"DjangoModel1",
"fields": [
{
"name": "field1",
"type": "CharField",
"args": "max_length=30"
},
{
"name": "field2",
"type": "TextField",
"args": "max_length=100"
}
],
"relationships": [
{
"name": "relationship1",
"type": "django.db.models.ForeignKey",
"to": "django.contrib.auth.models.User",
"args": "on_delete=models.CASCADE"
}
]
}
]
}
]
}
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
"name": "djangobuilder",
"version": "0.1.0",
"private": true,
"bin": {
"django-builder": "bin/django-builder"
},
"scripts": {
"dev": "vite --port 8080",
"build": "vite build",
Expand All @@ -18,6 +21,7 @@
"ci": "start-server-and-test test:e2e_serve http://localhost:8082 test:e2e"
},
"dependencies": {
"esm": "^3.2.25",
"firebase": "^9.1.0",
"firebase-tools": "^9.19.0",
"highlight.js": "^11.2.0",
Expand Down Expand Up @@ -81,7 +85,7 @@
"no-console": "off",
"no-empty-pattern": "error",
"no-unused-vars": [
"error",
"off",
{
"vars": "all",
"args": "after-used",
Expand Down
24 changes: 24 additions & 0 deletions script/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/bash

set -ex

./bin/django-builder example-project.json output.tar

tar -xvf output.tar

cd DjangoProject
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
python manage.py makemigrations
python manage.py migrate
python manage.py runserver &
ID=$!
curl --connect-timeout 5 \
--retry-connrefused \
--max-time 5 \
--retry 5 \
--retry-delay 2 \
--retry-max-time 60 \
'http://127.0.0.1:8000'
kill ${ID}
133 changes: 133 additions & 0 deletions src/cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
import Renderer from "./django/rendering"
import fs from 'fs';
import { logger } from "firebase-tools";

class Colors {
static Reset = "\x1b[0m";
static Bright = "\x1b[1m"
static Dim = "\x1b[2m"
static Underscore = "\x1b[4m"
static Blink = "\x1b[5m"
static Reverse = "\x1b[7m"
static Hidden = "\x1b[8m"

static FgBlack = "\x1b[30m"
static FgRed = "\x1b[31m"
static FgGreen = "\x1b[32m"
static FgYellow = "\x1b[33m"
static FgBlue = "\x1b[34m"
static FgMagenta = "\x1b[35m"
static FgCyan = "\x1b[36m"
static FgWhite = "\x1b[37m"

static BgBlack = "\x1b[40m"
static BgRed = "\x1b[41m"
static BgGreen = "\x1b[42m"
static BgYellow = "\x1b[43m"
static BgBlue = "\x1b[44m"
static BgMagenta = "\x1b[45m"
static BgCyan = "\x1b[46m"
static BgWhite = "\x1b[47m"
}


function project_data_to_store(project_data) {

const apps = project_data.apps;

const appData = {};
const modelData = {};
const fields = {};
const relationships = {};

apps.forEach((app, i) => {
const singleAppData = {
name: app.name,
models: {}
}
Object.assign(appData, { [`${i}`]: singleAppData});

app.models.forEach((model, i) => {
const singleModelData = {
...model,
parents: model.parents || [],
fields: {},
relationships: {},
}
singleAppData.models[`${model.name}`] = true

Object.assign(modelData, { [`${model.name}`]: singleModelData});

model.fields.forEach((field, i) => {
const singleFieldData = {
data: () => {
return {
...field,
args: field.args || "",
}
},
}
Object.assign(fields, { [`${field.name}`]: singleFieldData});
singleModelData.fields[`${field.name}`] = true
})

model.relationships.forEach((relationship, i) => {
const singleRelationshipData = {
data: () => {
return {
...relationship,
to: relationship.to,
}
},
}
Object.assign(relationships, { [`${relationship.name}`]: singleRelationshipData});
singleModelData.relationships[`${relationship.name}`] = true
})

})

})

const projectData = {
...project_data,
apps: apps.map((_, i) => { return { [`${i}`]: true} }),
}

return {
getters: {
projectData: () => projectData,
appData: (app_id) => appData[app_id],
modelData: (model_id) => modelData[model_id],
fields: () => fields,
relationships: () => relationships,
ordered_models: () => {
return Object.values(modelData)
}
}
}
}

export function cli(args) {
if (args.length != 4){
console.error(`${Colors.FgRed}Please supply an input json file and output tar file.${Colors.Reset}`)
console.error(`${Colors.FgRed}Usage: ./bin/django-builder example-project.json output.tar.${Colors.Reset}`)
return
}

const input_file = args[2];
const output_file = args[3];

let rawdata = fs.readFileSync(input_file);
let project_json = JSON.parse(rawdata);

const store = project_data_to_store(project_json)
const renderer = new Renderer(store);
const tar_content = renderer.tarball_content()

try {
fs.writeFileSync(output_file, tar_content);
console.log(`${Colors.FgGreen}File written to ${output_file} ${Colors.Reset}`); //cyan
} catch (err) {
console.error(err);
}
}
6 changes: 3 additions & 3 deletions src/components/DirectoryView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,10 @@
</template>

<script>
import store from "../store";
import Renderer from '@/django/rendering';
import Renderer from '@/django/rendering'
const renderer = new Renderer()
const renderer = new Renderer(store)
export default {
props: ['id'],
Expand Down
5 changes: 3 additions & 2 deletions src/components/Project.vue
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,9 @@ import { DEFAULT_DJANGO_VERSION } from '@/django'
import {schemas} from '@/schemas'
import {showDeleteDialog, showFormDialog} from '@/dialogs/'
import 'highlight.js/styles/a11y-light.css'
import store from "../store";
const renderer = new Renderer()
const renderer = new Renderer(store)
export default {
props: ['id'],
Expand Down Expand Up @@ -205,7 +206,7 @@ export default {
event_label: this.name,
value: 1
})
const url = renderer.as_tarball(this.id)
const url = renderer.tarball_url(this.id)
const link = document.createElement("a")
link.download = this.name + '.tar'
link.href = url
Expand Down
3 changes: 2 additions & 1 deletion src/django/python/urls.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""XXX_PROJECT_NAME_XXX URL Configuration
"""
XXX_PROJECT_NAME_XXX URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/2.1/topics/http/urls/
Expand Down
Loading

0 comments on commit 52f36e0

Please sign in to comment.