project.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. export * as Project from "./project"
  2. import { Context, Effect, Layer, Schema } from "effect"
  3. import path from "path"
  4. import { AbsolutePath, withStatics } from "./schema"
  5. import { AppFileSystem } from "./filesystem"
  6. import { Git } from "./git"
  7. import { Hash } from "./util/hash"
  8. export const ID = Schema.String.pipe(
  9. Schema.brand("Project.ID"),
  10. withStatics((schema) => ({
  11. global: schema.make("global"),
  12. })),
  13. )
  14. export type ID = typeof ID.Type
  15. export const Vcs = Schema.Union([
  16. Schema.Struct({
  17. type: Schema.Literal("git"),
  18. store: AbsolutePath,
  19. }),
  20. ])
  21. export type Vcs = typeof Vcs.Type
  22. export class Info extends Schema.Class<Info>("Project.Info")({
  23. id: ID,
  24. vcs: Schema.optional(Vcs),
  25. }) {}
  26. export interface Interface {
  27. readonly resolve: (input: AbsolutePath) => Effect.Effect<
  28. {
  29. previous?: ID
  30. id: ID
  31. directory: AbsolutePath
  32. vcs?: Vcs
  33. },
  34. never
  35. >
  36. /**
  37. * Temporary bridge method for writing the resolved project ID to the repo-local cache.
  38. *
  39. * This exists while the old opencode project service and this core project
  40. * service work together: core resolves the ID, while the old service still owns
  41. * database migration and persistence. The old service should call this after it
  42. * finishes migrating from `resolve().previous` to `resolve().id`; once project
  43. * persistence moves into core, this separate bridge method can go away.
  44. */
  45. readonly commit: (input: { store: AbsolutePath; id: ID }) => Effect.Effect<void>
  46. }
  47. export class Service extends Context.Service<Service, Interface>()("@opencode/ProjectV2") {}
  48. export const layer = Layer.effect(
  49. Service,
  50. Effect.gen(function* () {
  51. const fs = yield* AppFileSystem.Service
  52. const git = yield* Git.Service
  53. const cached = Effect.fnUntraced(function* (dir: string) {
  54. return yield* fs.readFileString(path.join(dir, "opencode")).pipe(
  55. Effect.map((value) => value.trim()),
  56. Effect.map((value) => (value ? ID.make(value) : undefined)),
  57. Effect.catch(() => Effect.succeed(undefined)),
  58. )
  59. })
  60. const remote = Effect.fnUntraced(function* (repo: Git.Repo) {
  61. const origin = yield* git.remote(repo)
  62. if (!origin) return undefined
  63. const normalized = url(origin)
  64. if (!normalized) return undefined
  65. return ID.make(Hash.fast(`git-remote:${normalized}`))
  66. })
  67. function url(input: string) {
  68. const value = input.trim()
  69. if (!value) return undefined
  70. try {
  71. const parsed = new URL(value)
  72. if (parsed.protocol === "file:") return undefined
  73. return parts(parsed.hostname, parsed.pathname)
  74. } catch {
  75. const scp = value.match(/^([^@/:]+@)?([^/:]+):(.+)$/)
  76. if (scp) return parts(scp[2], scp[3])
  77. return undefined
  78. }
  79. }
  80. function parts(host: string, name: string) {
  81. const pathname = name
  82. .replace(/^\/+/, "")
  83. .replace(/\.git\/?$/, "")
  84. .replace(/\/+$/, "")
  85. if (!host || !pathname) return undefined
  86. return `${host.toLowerCase()}/${pathname}`
  87. }
  88. const root = Effect.fnUntraced(function* (repo: Git.Repo) {
  89. const root = (yield* git.roots(repo))[0]
  90. return root ? ID.make(root) : undefined
  91. })
  92. const resolve = Effect.fn("Project.resolve")(function* (input: AbsolutePath) {
  93. const repo = yield* git.find(input)
  94. if (!repo) return { id: ID.global, directory: input, vcs: undefined }
  95. const previous = yield* cached(repo.store)
  96. const id = previous ?? (yield* root(repo))
  97. return {
  98. previous,
  99. id: id ?? ID.global,
  100. directory: repo.directory,
  101. vcs: { type: "git" as const, store: repo.store },
  102. }
  103. })
  104. const commit = Effect.fn("Project.commit")(function* (input: { store: AbsolutePath; id: ID }) {
  105. yield* fs.writeFileString(path.join(input.store, "opencode"), input.id).pipe(Effect.ignore)
  106. })
  107. return Service.of({ resolve, commit })
  108. }),
  109. )
  110. export const defaultLayer = layer.pipe(Layer.provide(AppFileSystem.defaultLayer), Layer.provide(Git.defaultLayer))