Co-authored-by: Brendan Allan <14191578+Brendonovich@users.noreply.github.com> Co-authored-by: Kit Langton <kit.langton@gmail.com> Co-authored-by: opencode-agent[bot] <opencode-agent[bot]@users.noreply.github.com> Co-authored-by: Affan Ali <93028901+affanali2k3@users.noreply.github.com> Co-authored-by: affanali2k3 <affanalikhanxx@gmail.com> Co-authored-by: Frank <frank@anoma.ly> Co-authored-by: opencode-agent[bot] <219766164+opencode-agent[bot]@users.noreply.github.com> Co-authored-by: 𝓛𝓲𝓽𝓽𝓵𝓮 𝓕𝓻𝓪𝓷𝓴 <little-frank@opencord.local> Co-authored-by: Aiden Cline <63023139+rekram1-node@users.noreply.github.com> Co-authored-by: Jay V <air@live.ca> Co-authored-by: Dax Raad <d@ironbay.co> Co-authored-by: Aarav Sareen <96787824+arvsrn@users.noreply.github.com> Co-authored-by: OpeOginni <107570612+OpeOginni@users.noreply.github.com> Co-authored-by: Luke Parker <10430890+Hona@users.noreply.github.com> Co-authored-by: Ben Guthrie <benjee.012@gmail.com> Co-authored-by: Dax <mail@thdxr.com> Co-authored-by: Filip <34747899+neriousy@users.noreply.github.com> Co-authored-by: Max Anderson <max.a.anderson95@gmail.com> Co-authored-by: Brendan Allan <git@brendonovich.dev> Co-authored-by: Jack <jack@anoma.ly> Co-authored-by: Shoubhit Dash <shoubhit2005@gmail.com> Co-authored-by: Dustin Deus <deusdustin@gmail.com> Co-authored-by: starptech <starptech@starptechs-MBP.fritz.box> Co-authored-by: Aiden Cline <aidenpcline@gmail.com> Co-authored-by: usrnk1 <7547651+usrnk1@users.noreply.github.com> Co-authored-by: Jay <53023+jayair@users.noreply.github.com> Co-authored-by: runvip <164729189+runvip@users.noreply.github.com> Co-authored-by: opencode <opencode@sst.dev> Co-authored-by: Julian Coy <julian@ex-machina.co> Co-authored-by: Vladimir Glafirov <vglafirov@gitlab.com>
153 lines
5.2 KiB
TypeScript
153 lines
5.2 KiB
TypeScript
export * as ProjectV2 from "./project"
|
|
export * as Project from "./project"
|
|
|
|
import { Context, Effect, Layer, Schema } from "effect"
|
|
import path from "path"
|
|
import { AbsolutePath } from "./schema"
|
|
import { FSUtil } from "./fs-util"
|
|
import { Git } from "./git"
|
|
import { makeGlobalNode } from "./effect/app-node"
|
|
import { Hash } from "./util/hash"
|
|
import { ProjectDirectories } from "./project/directories"
|
|
import { ProjectSchema } from "./project/schema"
|
|
|
|
export const ID = ProjectSchema.ID
|
|
export type ID = ProjectSchema.ID
|
|
|
|
export const Vcs = ProjectSchema.Vcs
|
|
export type Vcs = ProjectSchema.Vcs
|
|
|
|
export const Current = ProjectSchema.Current
|
|
export type Current = ProjectSchema.Current
|
|
|
|
export const Directory = ProjectSchema.Directory
|
|
export type Directory = ProjectSchema.Directory
|
|
|
|
export class Info extends Schema.Class<Info>("Project.Info")({
|
|
id: ID,
|
|
}) {}
|
|
|
|
export const DirectoriesInput = ProjectSchema.DirectoriesInput
|
|
export type DirectoriesInput = typeof DirectoriesInput.Type
|
|
|
|
export const Directories = ProjectSchema.Directories
|
|
export type Directories = typeof Directories.Type
|
|
|
|
export interface Resolved {
|
|
readonly previous?: ID
|
|
readonly id: ID
|
|
readonly directory: AbsolutePath
|
|
readonly vcs?: Vcs
|
|
}
|
|
|
|
// Keep this filesystem-only; permission checks use it and should not execute VCS commands.
|
|
export const root = Effect.fn("Project.root")(function* (
|
|
fs: FSUtil.Interface,
|
|
input: AbsolutePath,
|
|
) {
|
|
return yield* fs.up({ targets: [".git"], start: input }).pipe(
|
|
Effect.map((matches) => matches[0] ? AbsolutePath.make(path.dirname(matches[0])) : undefined),
|
|
Effect.catch(() => Effect.succeed(undefined)),
|
|
)
|
|
})
|
|
|
|
export interface Interface {
|
|
readonly directories: (input: DirectoriesInput) => Effect.Effect<Directories>
|
|
readonly resolve: (input: AbsolutePath) => Effect.Effect<Resolved>
|
|
/**
|
|
* Temporary bridge method for writing the resolved project ID to the repo-local cache.
|
|
*
|
|
* This exists while the old opencode project service and this core project
|
|
* service work together: core resolves the ID, while the old service still owns
|
|
* database migration and persistence. The old service should call this after it
|
|
* finishes migrating from `resolve().previous` to `resolve().id`; once project
|
|
* persistence moves into core, this separate bridge method can go away.
|
|
*/
|
|
readonly commit: (input: { store: AbsolutePath; id: ID }) => Effect.Effect<void>
|
|
}
|
|
|
|
export class Service extends Context.Service<Service, Interface>()("@opencode/ProjectV2") {}
|
|
|
|
const layer = Layer.effect(
|
|
Service,
|
|
Effect.gen(function* () {
|
|
const fs = yield* FSUtil.Service
|
|
const git = yield* Git.Service
|
|
const projectDirectories = yield* ProjectDirectories.Service
|
|
|
|
const directories = Effect.fn("Project.directories")(function* (input: DirectoriesInput) {
|
|
return yield* projectDirectories.list(input.projectID)
|
|
})
|
|
|
|
const cached = Effect.fnUntraced(function* (dir: string) {
|
|
return yield* fs.readFileString(path.join(dir, "opencode")).pipe(
|
|
Effect.map((value) => value.trim()),
|
|
Effect.map((value) => (value ? ID.make(value) : undefined)),
|
|
Effect.catch(() => Effect.succeed(undefined)),
|
|
)
|
|
})
|
|
|
|
const remote = Effect.fnUntraced(function* (repo: Git.Repository) {
|
|
const origin = yield* git.remote.get(repo)
|
|
if (!origin) return undefined
|
|
const normalized = url(origin)
|
|
if (!normalized) return undefined
|
|
return ID.make(Hash.fast(`git-remote:${normalized}`))
|
|
})
|
|
|
|
function url(input: string) {
|
|
const value = input.trim()
|
|
if (!value) return undefined
|
|
|
|
try {
|
|
const parsed = new URL(value)
|
|
if (parsed.protocol === "file:") return undefined
|
|
return parts(parsed.hostname, parsed.pathname)
|
|
} catch {
|
|
const scp = value.match(/^([^@/:]+@)?([^/:]+):(.+)$/)
|
|
if (scp) return parts(scp[2], scp[3])
|
|
return undefined
|
|
}
|
|
}
|
|
|
|
function parts(host: string, name: string) {
|
|
const pathname = name
|
|
.replace(/^\/+/, "")
|
|
.replace(/\.git\/?$/, "")
|
|
.replace(/\/+$/, "")
|
|
if (!host || !pathname) return undefined
|
|
return `${host.toLowerCase()}/${pathname}`
|
|
}
|
|
|
|
const root = Effect.fnUntraced(function* (repo: Git.Repository) {
|
|
const root = (yield* git.history.rootCommits(repo))[0]
|
|
return root ? ID.make(root) : undefined
|
|
})
|
|
|
|
const resolve = Effect.fn("Project.resolve")(function* (input: AbsolutePath) {
|
|
const repo = yield* git.repo.discover(input)
|
|
if (!repo) return { id: ID.global, directory: AbsolutePath.make(path.parse(input).root), vcs: undefined }
|
|
|
|
const previous = yield* cached(repo.commonDirectory)
|
|
const id = (yield* remote(repo)) ?? previous ?? (yield* root(repo))
|
|
return {
|
|
previous,
|
|
id: id ?? ID.global,
|
|
directory: repo.worktree,
|
|
vcs: { type: "git" as const, store: repo.commonDirectory },
|
|
}
|
|
})
|
|
|
|
const commit = Effect.fn("Project.commit")(function* (input: { store: AbsolutePath; id: ID }) {
|
|
yield* fs.writeFileString(path.join(input.store, "opencode"), input.id).pipe(Effect.ignore)
|
|
})
|
|
|
|
return Service.of({ directories, resolve, commit })
|
|
}),
|
|
)
|
|
|
|
export const node = makeGlobalNode({
|
|
service: Service,
|
|
layer: layer,
|
|
deps: [FSUtil.node, Git.node, ProjectDirectories.node],
|
|
})
|