|
root / src / commands / depends.reef
depends.reef Reef 277 lines 6.6 KB
  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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/******************************************************************************
               __               ____                __
              / /   ___  ____ _/ __/_____________ _/ /__
             / /   / _ \/ __ `/ /_/ ___/ ___/ __ `/ / _ \
            / /___/  __/ /_/ / __(__  ) /__/ /_/ / /  __/
           /_____/\___/\__,_/_/ /____/\___/\__,_/_/\___/

   (C)opyright 2025, Leafscale, LLC -  https://www.leafscale.com

   Project: Zygaena
  Filename: depends.reef
   Authors: Chris Tusa <chris.tusa@leafscale.com>
   License: <see LICENSE file included with this source code>
Description: Depends command - show package dependency tree

******************************************************************************/

module commands.depends

import sys.args
import core.str
import core.port
import core.portindex
import core.database
import core.resolver
import types
import util.color
import exitcodes as ec

export
    fn execute(): int
end export

fn execute(): int
    let argc = args.count()

    if argc < 3
        print_usage()
        return ec.EXIT_USAGE()
    end if

    let pkg_name = args.get(2)

    // Check for --reverse flag (show what depends on this package)
    if args.has_flag("reverse") or args.has_flag("r")
        show_reverse_deps(pkg_name)
        return ec.EXIT_SUCCESS()
    end if

    // Show forward dependencies
    show_deps(pkg_name)
    return ec.EXIT_SUCCESS()
end execute

proc show_deps(name: string)
    // Find the port
    let result = port.find(name)
    if not result.success
        color.print_error("Port not found: " + name)
        return
    end if

    let p = result.port

    color.print_action("Dependencies for " + name + ":")
    println("")

    // Show runtime dependencies
    if p.deps.runtime_count == 0
        color.print_info("No runtime dependencies")
    else
        color.print_info("Runtime dependencies:")
        show_dep_tree(name, 0)
    end if

    println("")

    // Show build dependencies
    if p.deps.build_count > 0
        color.print_info("Build dependencies:")
        mut i = 0
        while i < p.deps.build_count
            print("  ")
            let dep = p.deps.build[i]
            print(dep)
            if database.is_installed(dep)
                println(" [installed]")
            else
                println("")
            end if
            i = i + 1
        end while
    end if
end show_deps

proc show_dep_tree(name: string, depth: int)
    if depth > 10
        return
    end if

    let result = port.find(name)
    if not result.success
        return
    end if

    let p = result.port

    mut i = 0
    while i < p.deps.runtime_count
        let dep = p.deps.runtime[i]

        // Print with indentation
        print_indent(depth)
        print(dep)
        if database.is_installed(dep)
            println(" [installed]")
        else
            println("")
        end if

        // Recurse
        show_dep_tree(dep, depth + 1)

        i = i + 1
    end while
end show_dep_tree

proc show_reverse_deps(name: string)
    color.print_action("Packages that depend on " + name + ":")
    println("")

    mut found = 0

    // Try ports index first (fast path)
    if portindex.port_index_exists()
        mut results: [portindex.PortIndexEntry] = new [portindex.PortIndexEntry](512)
        let result_count = portindex.find_reverse_deps(name, results, 512)

        mut i = 0
        while i < result_count
            let entry = results[i]
            print("  ")
            print(entry.category)
            print("/")
            print(entry.name)
            if database.is_installed(entry.name)
                println(" [installed]")
            else
                println("")
            end if
            found = found + 1
            i = i + 1
        end while
    else
        // Fall back to filesystem scan
        mut cats: [string] = new [string](32)
        let cat_count = port.get_categories(cats, 32)

        mut i = 0
        while i < cat_count
            let category = cats[i]
            mut ports: [string] = new [string](256)
            let port_count = port.list_category(category, ports, 256)

            mut j = 0
            while j < port_count
                let port_name = ports[j]
                if depends_on(port_name, name)
                    print("  ")
                    print(category)
                    print("/")
                    print(port_name)
                    if database.is_installed(port_name)
                        println(" [installed]")
                    else
                        println("")
                    end if
                    found = found + 1
                end if
                j = j + 1
            end while

            i = i + 1
        end while
    end if

    if found == 0
        color.print_info("No packages depend on " + name)
    else
        println("")
        color.print_info("Total: " + int_to_str(found) + " package(s)")
    end if
end show_reverse_deps

fn depends_on(pkg_name: string, dep_name: string): bool
    let result = port.find(pkg_name)
    if not result.success
        return false
    end if

    let p = result.port

    // Check runtime deps
    mut i = 0
    while i < p.deps.runtime_count
        if p.deps.runtime[i] == dep_name
            return true
        end if
        i = i + 1
    end while

    // Check build deps
    i = 0
    while i < p.deps.build_count
        if p.deps.build[i] == dep_name
            return true
        end if
        i = i + 1
    end while

    return false
end depends_on

proc print_indent(depth: int)
    mut i = 0
    while i < depth
        print("  ")
        i = i + 1
    end while
    print("  ")  // Base indent
end print_indent

proc print_usage()
    println("Usage: coral depends <package> [options]")
    println("")
    println("Show dependency tree for a package.")
    println("")
    println("Arguments:")
    println("  <package>    Name of package to show dependencies for")
    println("")
    println("Options:")
    println("  -r, --reverse    Show packages that depend on this package")
    println("")
    println("Examples:")
    println("  coral depends vim")
    println("  coral depends vim --reverse")
end print_usage

// Helper: convert int to string
fn int_to_str(n: int): string
    if n == 0
        return "0"
    end if

    mut negative = false
    mut value = n
    if n < 0
        negative = true
        value = 0 - n
    end if

    mut result = ""
    while value > 0
        let digit = value % 10
        result = str.concat(str.substring("0123456789", digit, 1), result)
        value = value / 10
    end while

    if negative
        result = str.concat("-", result)
    end if

    return result
end int_to_str

end module