File size: 4,506 Bytes
e6073d4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# Initialize the development environment for the project
# ---
PROJECT_ROOT=$(git rev-parse --show-toplevel)

# Default to minimal installation unless --full flag passed
INSTALL_FULL=false
DEVCONTAINER=false
for arg in "$@"
do
    if [ "$arg" = "--full" ]
    then
        INSTALL_FULL=true
    fi
    if [ "$arg" = "--devcontainer" ]
    then
        DEVCONTAINER=true
    fi
done

if [ "$DEVCONTAINER" = true ]; then
    echo "Dev container setup - using pre-installed dependencies..."
    cd "$PROJECT_ROOT"

    # Use devcontainer launch.json
    mkdir -p .vscode && cp .devcontainer/launch.json .vscode/launch.json

    # Install Server App using pre-installed dependencies
    echo "Setup Server App with UV. Use pre-installed dependencies in $UV_PROJECT_ENVIRONMENT."
    sed -i "s/dynamic = \\[\"version\"\\]/version = \"$VERSION\"/" pyproject.toml
    cp /opt/uv.lock.linux uv.lock
    uv sync --all-extras

    # Install Web App using cached dependencies
    echo "Setup Web App with Bun. Use pre-installed dependencies in /opt/khoj_web."
    cd "$PROJECT_ROOT/src/interface/web"
    ln -sf /opt/khoj_web/node_modules node_modules
    bun install && bun run ciexport
else
    # Standard setup
    echo "Installing Server App..."
    cd "$PROJECT_ROOT"
    if command -v uv &> /dev/null
    then
        uv venv
        uv sync --all-extras
    else
        python3 -m venv .venv && . .venv/bin/activate
        python3 -m pip install -e '.[dev]'
    fi

    echo "Installing Web App..."
    cd "$PROJECT_ROOT/src/interface/web"
    if command -v bun &> /dev/null
    then
        echo "using Bun."
        bun install && bun run export
    else
        echo "using Yarn."
        yarn install && yarn export
    fi
fi

# Install Obsidian App
# ---
if [ "$INSTALL_FULL" = true ] ; then
    echo "Installing Obsidian App..."
    cd $PROJECT_ROOT/src/interface/obsidian
    yarn install
fi

# Install Desktop App
# ---
if [ "$INSTALL_FULL" = true ] ; then
    echo "Installing Desktop App..."
    cd $PROJECT_ROOT/src/interface/desktop
    yarn install
fi

# Install pre-commit hooks
# ----
echo "Installing pre-commit hooks..."

# Setup pre-commit hooks using the pre-commit package
pre-commit install -t pre-push -t pre-commit

# Run Prettier on web app
cat << 'EOF' > temp_pre_commit
# Run Prettier for Web App
# -------------------------

# Function to check if color output is possible
can_use_color() {
    if [ -t 1 ] && command -v tput >/dev/null 2>&1 && tput colors >/dev/null 2>&1; then
        return 0
    else
        return 1
    fi
}

# Function to print colored text if possible
print_color() {
    if can_use_color; then
        tput setab "$1"
        printf "%s" "$2"
        tput sgr0
    else
        printf "%s" "$2"
    fi
}

print_status() {
    local status="$1"
    local color="$2"
    printf "prettier%-64s" "..."
    print_color "$color" "$status"
    printf "\n"
}

PROJECT_ROOT=$(git rev-parse --show-toplevel)
# Get the list of staged files
FILES=$(git diff --cached --name-only --diff-filter=ACMR | grep '^src/interface/web/' | sed 's| |\\ |g')
if [ -z "$FILES" ]; then
    if [ -t 1 ]; then
        print_status "Skipped" 6
    else
        echo "prettier.....................................................Skipped"
    fi
else
    # Run prettier on staged files
    echo "$FILES" | xargs $PROJECT_ROOT/src/interface/web/node_modules/.bin/prettier --ignore-unknown --write

    # Check if any files were modified by prettier
    MODIFIED=$(git diff --name-only -- $FILES)
    if [ -n "$MODIFIED" ]; then
        if [ -t 1 ]; then
            print_status "Modified" 1
        else
            echo "prettier.....................................................Modified"
        fi
        exit 1
    fi

    # Add back the modified/prettified files to staging
    # echo "$FILES" | xargs git add

    # Show the user if changes were made
    if [ -t 1 ]; then
        print_status "Passed" 2
    else
        echo "prettier.....................................................Passed"
    fi
fi
EOF

# Prepend the new content to the existing pre-commit file
cat temp_pre_commit "$(git rev-parse --git-dir)/hooks/pre-commit" > temp_combined_pre_commit

# Replace the old pre-commit file with the new combined one
mv temp_combined_pre_commit "$(git rev-parse --git-dir)/hooks/pre-commit"

# Clean up
# ---

# Remove the temporary pre-commit file
rm temp_pre_commit

# Make sure the pre-commit hook is executable
chmod +x "$(git rev-parse --git-dir)/hooks/pre-commit"