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

Better project generation #133

Merged
merged 1 commit into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 = "john@doe.com"},
]
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"