Skip to content

Commit

Permalink
Merge pull request #133 from mekanix/feature/project
Browse files Browse the repository at this point in the history
Better project generation
  • Loading branch information
mekanix authored Apr 3, 2024
2 parents c733f21 + da51d5a commit ac9f024
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 131 deletions.
126 changes: 6 additions & 120 deletions bin/freenit.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ set -e

help() {
echo "Usage: $0 <type> <name>"
echo " type: project, backend, react, svelte"
echo " type: project, backend, frontend"
echo " name: name used everywhere in the project"

}
Expand All @@ -24,7 +24,7 @@ if [ -z "${TYPE}" ]; then
fi

case "${TYPE}" in
project|backend|react|svelte)
project|backend|frontend)
;;
*)
help >&2
Expand Down Expand Up @@ -52,10 +52,12 @@ backend() {
*BSD)
${SED_CMD} '' -e "s/NAME/${NAME}/g" setup.py
${SED_CMD} '' -e "s/NAME/${NAME}/g" main.py
${SED_CMD} '' -e "s/PROJECT/${NAME}/g" pyproject.toml
;;
*)
${SED_CMD} -e "s/NAME/${NAME}/g" setup.py
${SED_CMD} -e "s/NAME/${NAME}/g" main.py
${SED_CMD} -e "s/PROJECT/${NAME}/g" pyproject.toml
;;
esac
mv project ${NAME}
Expand Down Expand Up @@ -286,115 +288,7 @@ vars.mk
EOF
}

react() {
npm init vite@latest "${NAME}" -- --template react-ts
cd "${NAME}"
npm install @freenit-framework/axios react-router-dom @mdi/js
frontend_common

rm src/App.* src/index.css src/logo.svg
mkdir src/routes
cat >src/routing.tsx<<EOF
import React, { Fragment } from 'react'
import { Route, Routes } from 'react-router-dom'
const PRESERVED = import.meta.globEager('/src/routes/(_app|404).tsx')
const ROUTES = import.meta.globEager('/src/routes/**/[a-z[]*.tsx')
const preserved: { [key: string]: any } = Object.keys(PRESERVED).reduce((preserved, file) => {
const key = file.replace(/\/src\/routes\/|\.tsx$/g, '')
return { ...preserved, [key]: PRESERVED[file].default }
}, {})
const routes = Object.keys(ROUTES).map((route) => {
const path = route
.replace(/\/src\/routes|index|\.tsx$/g, '')
.replace(/\[\.{3}.+\]/, '*')
.replace(/\[(.+)\]/, ':')
return { path, component: ROUTES[route].default }
})
export const Routing = () => {
const NotFound = preserved?.['404'] || Fragment
return (
<Routes>
{routes.map(({ path, component: Component = Fragment }) => (
<Route key={path} path={path} element={<Component />} />
))}
<Route path="*" element={<NotFound />} />
</Routes>
)
}
EOF

cat >src/main.tsx<<EOF
import React, { StrictMode } from 'react'
import { createRoot } from 'react-dom/client'
import { BrowserRouter } from 'react-router-dom'
import 'chota'
import { Routing } from './routing'
const container = document.getElementById('root');
const root = createRoot(container!);
root.render(
<StrictMode>
<BrowserRouter>
<Routing />
</BrowserRouter>
</StrictMode>
);
EOF

cat >src/routes/index.tsx<<EOF
export default function Landing() {
return (
<div>Hello World!</div>
)
}
EOF

case `uname` in
*BSD)
${SED_CMD} '' -e "s/})//g" vite.config.ts
${SED_CMD} '' -e "s/plugins: \(.*\)/plugins: \1,/g" vite.config.ts
;;
*)
${SED_CMD} -e "s/})//g" vite.config.ts
${SED_CMD} -e "s/plugins: \(.*\)/plugins: \1,/g" vite.config.ts
;;
esac
cat >>vite.config.ts<<EOF
server: {
proxy: {
'/api': {
target: process.env.BACKEND_URL,
changeOrigin: true
}
}
}
})
EOF

cd bin
cat >devel.sh<<EOF
#!/bin/sh
BIN_DIR=\`dirname \$0\`
. "\${BIN_DIR}/common.sh"
setup
echo "Frontend"
echo "========"
env BACKEND_URL=\${BACKEND_URL} npm run dev -- --host 0.0.0.0
EOF
chmod +x devel.sh
cd ..
}

svelte() {
frontend() {
npm create svelte@latest "${NAME}"
cd "${NAME}"
case `uname` in
Expand Down Expand Up @@ -698,15 +592,7 @@ EOF
mv "${NAME}" backend

echo "Creating frontend"
FRONTEND_TYPE=${FRONTEND_TYPE:=svelte}
if [ "${FRONTEND_TYPE}" = "svelte" ]; then
svelte
elif [ "${FRONTEND_TYPE}" = "react" ]; then
react
else
help >&2
exit 1
fi
frontend
mv "${NAME}" frontend
cd ..
}
Expand Down
6 changes: 5 additions & 1 deletion freenit/cli.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import pathlib
import subprocess

from prompt_toolkit import prompt


def main():
path = pathlib.Path(__file__).parent.resolve()
project_name = prompt("Name of the project: ")
subprocess.run(["bin/freenit.sh", "project", project_name])
subprocess.run([f"{path}/bin/freenit.sh", "project", project_name])
Empty file added freenit/project/LICENSE
Empty file.
1 change: 1 addition & 0 deletions freenit/project/project/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__version__ = "0.0.1"
19 changes: 9 additions & 10 deletions freenit/project/project/app.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
from contextlib import asynccontextmanager
from fastapi import FastAPI

import freenit.config

from .api import api
from .config import getConfig

config = getConfig()
app = FastAPI()
config = freenit.config.getConfig()


@app.on_event("startup")
async def startup() -> None:
@asynccontextmanager
async def lifespan(_: FastAPI):
if not config.database.is_connected:
await config.database.connect()


@app.on_event("shutdown")
async def shutdown() -> None:
yield
if config.database.is_connected:
await config.database.disconnect()


app.mount("/api/v1", api)
app = FastAPI(lifespan=lifespan)
app.mount(config.api_root, api)
38 changes: 38 additions & 0 deletions freenit/project/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

[project]
name = "NAME"
dynamic = ["version"]
description = "REST API framework based on FastAPI"
readme = "README.md"
license = {file = "LICENSE"}
requires-python = ">=3.8"
dependencies = [
"freenit[ormar]",
]
authors = [
{name = "John Doe", email = "[email protected]"},
]
classifiers = [
"Development Status :: 4 - Beta",
"Intended Audience :: Developers",
"Topic :: Internet :: WWW/HTTP",
"Environment :: Web Environment",
"Programming Language :: Python"
]

[project.optional-dependencies]
beanie = ["freenit[beanie]"]
dev = ["freenit[dev]"]
ldap = ["freenit[ldap]"]
ormar = ["freenit[ormar]"]
test = ["freenit[test]"]

[project.urls]
Homepage = "https://freenit.org"
Repository = "https://github.com/freenit-framework/backend"

[tool.hatch.version]
path = "PROJECT/__init__.py"

0 comments on commit ac9f024

Please sign in to comment.