aboutsummaryrefslogtreecommitdiffstats
path: root/lua/dep.lua
blob: bf466b07e75f167581257207beac9a1ebf591042 (plain) (blame)
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
local logger = require("dep/log")
local proc = require("dep/proc")

logger:open()

local initialized, config_path, base_dir
local packages, package_roots

local function get_name(id)
  local name = id:match("^[%w-_.]+/([%w-_.]+)$")
  if name then
    return name
  else
    error(string.format('invalid name "%s"; must be in the format "user/package"', id))
  end
end

local function link_dependency(parent, child)
  if not parent.dependents[child.id] then
    parent.dependents[child.id] = child
    parent.dependents[#parent.dependents + 1] = child
  end

  if not child.dependencies[parent.id] then
    child.dependencies[parent.id], child.root = parent, false
    child.dependencies[#child.dependencies + 1] = parent
  end
end

local function register(spec, overrides)
  overrides = overrides or {}

  if type(spec) ~= "table" then
    spec = { spec }
  end

  local id = spec[1]
  local package = packages[id]

  if not package then
    package = {
      id = id,
      enabled = true,
      exists = false,
      added = false,
      configured = false,
      loaded = false,
      on_setup = {},
      on_config = {},
      on_load = {},
      root = true,
      dependencies = {}, -- inward edges
      dependents = {}, -- outward edges
    }

    packages[id] = package
    packages[#packages + 1] = package
  end

  local prev_dir = package.dir -- optimization

  -- meta
  package.name = spec.as or package.name or get_name(id)
  package.url = spec.url or package.url or ("https://github.com/" .. id .. ".git")
  package.branch = spec.branch or package.branch
  package.dir = base_dir .. package.name
  package.pin = overrides.pin or spec.pin or package.pin
  package.enabled = not overrides.disable and not spec.disable and package.enabled

  if prev_dir ~= package.dir then
    package.exists = vim.fn.isdirectory(package.dir) ~= 0
    package.configured = package.exists
  end

  package.on_setup[#package.on_setup + 1] = spec.setup
  package.on_config[#package.on_config + 1] = spec.config
  package.on_load[#package.on_load + 1] = spec[2]

  if type(spec.requires) == "table" then
    for i = 1, #spec.requires do
      link_dependency(register(spec.requires[i]), package)
    end
  elseif spec.requires then
    link_dependency(register(spec.requires), package)
  end

  if type(spec.deps) == "table" then
    for i = 1, #spec.deps do
      link_dependency(package, register(spec.deps[i]))
    end
  elseif spec.deps then
    link_dependency(package, register(spec.deps))
  end

  return package
end

local function register_recursive(list, overrides)
  overrides = overrides or {}
  overrides = {
    pin = overrides.pin or list.pin,
    disable = overrides.disable or list.disable,
  }

  for i = 1, #list do
    local ok, err = pcall(register, list[i], overrides)
    if not ok then
      error(string.format("%s (spec=%s)", err, vim.inspect(list[i])))
    end
  end

  if list.modules then
    for i = 1, #list.modules do
      local name, module = "<unnamed module>", list.modules[i]

      if type(module) == "string" then
        name, module = module, require(module)
      end

      name = module.name or name

      local ok, err = pcall(register_recursive, module, overrides)
      if not ok then
        error(string.format("%s <- %s", err, name))
      end
    end
  end
end

local function sort_dependencies()
  local function compare(a, b)
    local a_deps = #a.dependencies
    local b_deps = #b.dependencies

    if a_deps == b_deps then
      return a.id < b.id
    else
      return a_deps < b_deps
    end
  end

  table.sort(packages, compare)

  for i = 1, #packages do
    table.sort(packages[i].dependencies, compare)
    table.sort(packages[i].dependents, compare)
  end
end

local function find_cycle()
  local index = 0
  local indexes = {}
  local lowlink = {}
  local stack = {}

  -- use tarjan algorithm to find circular dependencies (strongly connected components)
  local function connect(package)
    indexes[package.id], lowlink[package.id] = index, index
    stack[#stack + 1], stack[package.id] = package, true
    index = index + 1

    for i = 1, #package.dependents do
      local dependent = package.dependents[i]

      if not indexes[dependent.id] then
        local cycle = connect(dependent)
        if cycle then
          return cycle
        else
          lowlink[package.id] = math.min(lowlink[package.id], lowlink[dependent.id])
        end
      elseif stack[dependent.id] then
        lowlink[package.id] = math.min(lowlink[package.id], indexes[dependent.id])
      end
    end

    if lowlink[package.id] == indexes[package.id] then
      local cycle = { package }
      local node

      repeat
        node = stack[#stack]
        stack[#stack], stack[node.id] = nil, nil
        cycle[#cycle + 1] = node
      until node == package

      -- a node is by definition strongly connected to itself
      -- ignore single-node components unless it explicitly specified itself as a dependency
      if #cycle > 2 or package.dependents[package.id] then
        return cycle
      end
    end
  end

  for i = 1, #packages do
    local package = packages[i]

    if not indexes[package.id] then
      local cycle = connect(package)
      if cycle then
        return cycle
      end
    end
  end
end

local function ensure_acyclic()
  local cycle = find_cycle()

  if cycle then
    local names = {}
    for i = 1, #cycle do
      names[i] = cycle[i].id
    end
    error("circular dependency detected in package graph: " .. table.concat(names, " -> "))
  end
end

local function find_roots()
  for i = 1, #packages do
    local package = packages[i]
    if package.root then
      package_roots[#package_roots + 1] = package
    end
  end
end

local function run_hooks(package, type)
  local hooks = package[type]

  for i = 1, #hooks do
    local ok, err = pcall(hooks[i])
    if not ok then
      package.error = true
      return false, err
    end
  end

  if #hooks ~= 0 then
    logger:log(
      "hook",
      string.format("ran %d %s %s for %s", #hooks, type, #hooks == 1 and "hook" or "hooks", package.id)
    )
  end

  return true
end

local function ensure_added(package)
  if not package.added then
    local ok, err = pcall(vim.cmd, "packadd " .. package.name)
    if ok then
      package.added = true
      logger:log("vim", string.format("packadd completed for %s", package.id))
    else
      package.error = true
      return false, err
    end
  end

  return true
end

local function configure_recursive(package)
  if not package.exists or not package.enabled or package.error then
    return
  end

  for i = 1, #package.dependencies do
    if not package.dependencies[i].configured then
      return
    end
  end

  local propagate = false

  if not package.configured then
    local ok, err = run_hooks(package, "on_setup")
    if not ok then
      logger:log("error", string.format("failed to set up %s; reason: %s", package.id, err))
      return
    end

    ok, err = ensure_added(package)
    if not ok then
      logger:log("error", string.format("failed to configure %s; reason: %s", package.id, err))
      return
    end

    ok, err = run_hooks(package, "on_config")
    if not ok then
      logger:log("error", string.format("failed to configure %s; reason: %s", package.id, err))
      return
    end

    package.configured, package.loaded = true, false
    propagate = true

    logger:log("config", string.format("configured %s", package.id))
  end

  for i = 1, #package.dependents do
    local dependent = package.dependents[i]

    dependent.configured = dependent.configured and not propagate
    configure_recursive(dependent)
  end
end

local function load_recursive(package)
  if not package.exists or not package.enabled or package.error then
    return
  end

  for i = 1, #package.dependencies do
    if not package.dependencies[i].loaded then
      return
    end
  end

  local propagate = false

  if not package.loaded then
    local ok, err = ensure_added(package)
    if not ok then
      logger:log("error", string.format("failed to configure %s; reason: %s", package.id, err))
      return
    end

    ok, err = run_hooks(package, "on_load")
    if not ok then
      logger:log("error", string.format("failed to load %s; reason: %s", package.id, err))
      return
    end

    package.loaded = true
    propagate = true

    logger:log("load", string.format("loaded %s", package.id))
  end

  for i = 1, #package.dependents do
    local dependent = package.dependents[i]

    dependent.loaded = dependent.loaded and not propagate
    load_recursive(dependent, force)
  end
end

local function reload_meta()
  local ok, err = pcall(
    vim.cmd,
    [[
      silent! helptags ALL
      silent! UpdateRemotePlugins
    ]]
  )

  if ok then
    logger:log("vim", "reloaded helptags and remote plugins")
  else
    logger:log("error", string.format("failed to reload helptags and remote plugins; reason: %s", err))
  end
end

local function reload_all()
  -- clear all errors and try again
  for i = 1, #packages do
    packages[i].error = false
  end

  for i = 1, #package_roots do
    configure_recursive(package_roots[i])
  end

  for i = 1, #package_roots do
    load_recursive(package_roots[i])
  end

  reload_meta()
end

local function clean()
  vim.loop.fs_scandir(
    base_dir,
    vim.schedule_wrap(function(err, handle)
      if err then
        logger:log("error", string.format("failed to clean; reason: %s", err))
      else
        local queue = {}

        while handle do
          local name = vim.loop.fs_scandir_next(handle)
          if name then
            queue[name] = base_dir .. name
          else
            break
          end
        end

        for i = 1, #packages do
          queue[packages[i].name] = nil
        end

        for name, dir in pairs(queue) do
          -- todo: make this async
          local ok = vim.fn.delete(dir, "rf")
          if ok then
            logger:log("clean", string.format("deleted %s", name))
          else
            logger:log("error", string.format("failed to delete %s", name))
          end
        end
      end
    end)
  )
end

local function sync(package, cb)
  if not package.enabled then
    return
  end

  if package.exists then
    if package.pin then
      return
    end

    local function log_err(err)
      logger:log("error", string.format("failed to update %s; reason: %s", package.id, err))
    end

    proc.git_rev_parse(package.dir, "HEAD", function(err, before)
      if err then
        log_err(before)
        cb(err)
      else
        proc.git_fetch(package.dir, "origin", package.branch or "HEAD", function(err, message)
          if err then
            log_err(message)
            cb(err)
          else
            proc.git_rev_parse(package.dir, "FETCH_HEAD", function(err, after)
              if err then
                log_err(after)
                cb(err)
              elseif before == after then
                logger:log("skip", string.format("skipped %s", package.id))
                cb(err)
              else
                proc.git_reset(package.dir, after, function(err, message)
                  if err then
                    log_err(message)
                  else
                    package.added, package.configured = false, false
                    logger:log("update", string.format("updated %s; %s -> %s", package.id, before, after))
                  end

                  cb(err)
                end)
              end
            end)
          end
        end)
      end
    end)
  else
    proc.git_clone(package.dir, package.url, package.branch, function(err, message)
      if err then
        logger:log("error", string.format("failed to install %s; reason: %s", package.id, message))
      else
        package.exists, package.added, package.configured = true, false, false
        logger:log("install", string.format("installed %s", package.id))
      end

      cb(err)
    end)
  end
end

local function sync_list(list)
  local progress = 0
  local has_errors = false

  local function done(err)
    progress = progress + 1
    has_errors = has_errors or err

    if progress == #list then
      clean()
      reload_all()

      if has_errors then
        logger:log("error", "there were errors during sync; see :messages or :DepLog for more information")
      end
    end
  end

  for i = 1, #list do
    sync(list[i], done)
  end
end

local function print_list(list)
  local buffer = vim.api.nvim_create_buf(true, true)
  local line = 0
  local indent = 0

  local function print(chunks)
    local concat = {}
    local column = 0

    for i = 1, indent do
      concat[#concat + 1] = "  "
      column = column + 2
    end

    if not chunks then
      chunks = {}
    elseif type(chunks) == "string" then
      chunks = { { chunks } }
    end

    for i = 1, #chunks do
      local chunk = chunks[i]
      concat[#concat + 1] = chunk[1]
      chunk.offset, column = column, column + #chunk[1]
    end

    vim.api.nvim_buf_set_lines(buffer, line, -1, false, { table.concat(concat) })

    for i = 1, #chunks do
      local chunk = chunks[i]
      if chunk[2] then
        vim.api.nvim_buf_add_highlight(buffer, -1, chunk[2], line, chunk.offset, chunk.offset + #chunk[1])
      end
    end

    line = line + 1
  end

  print("Installed packages:")
  indent = 1

  local loaded = {}

  local function dry_load(package)
    for i = 1, #package.dependencies do
      if not loaded[package.dependencies[i].id] then
        return
      end
    end

    loaded[package.id] = true

    local line = {
      { "- ", "Comment" },
      { package.id, "Underlined" },
    }

    if not package.exists then
      line[#line + 1] = { " *not installed", "Comment" }
    end

    if not package.loaded then
      line[#line + 1] = { " *not loaded", "Comment" }
    end

    if not package.enabled then
      line[#line + 1] = { " *disabled", "Comment" }
    end

    if package.pin then
      line[#line + 1] = { " *pinned", "Comment" }
    end

    print(line)

    for i = 1, #package.dependents do
      dry_load(package.dependents[i])
    end
  end

  for i = 1, #package_roots do
    dry_load(package_roots[i])
  end

  indent = 0
  print()
  print("Dependency graph:")

  local function walk_graph(package)
    indent = indent + 1

    print({
      { "| ", "Comment" },
      { package.id, "Underlined" },
    })

    for i = 1, #package.dependents do
      walk_graph(package.dependents[i])
    end

    indent = indent - 1
  end

  for i = 1, #package_roots do
    walk_graph(package_roots[i])
  end

  vim.api.nvim_buf_set_name(buffer, "packages.dep")
  vim.api.nvim_buf_set_option(buffer, "bufhidden", "wipe")
  vim.api.nvim_buf_set_option(buffer, "modifiable", false)

  vim.cmd("sp")
  vim.api.nvim_win_set_buf(0, buffer)
end

vim.cmd([[
  command! DepSync lua require("dep").sync()
  command! DepReload lua require("dep").reload()
  command! DepClean lua require("dep").clean()
  command! DepList lua require("dep").list()
  command! DepLog lua require("dep").open_log()
  command! DepConfig lua require("dep").open_config()
]])

local function wrap_api(name, fn)
  return function(...)
    if initialized then
      local ok, err = pcall(fn, ...)
      if not ok then
        logger:log("error", err)
      end
    else
      logger:log("error", string.format("cannot call %s; dep is not initialized", name))
    end
  end
end

--todo: prevent multiple execution of async routines
return setmetatable({
  sync = wrap_api("dep.sync", function()
    sync_list(packages)
  end),

  reload = wrap_api("dep.reload", reload_all),
  clean = wrap_api("dep.clean", clean),

  list = wrap_api("dep.list", function()
    print_list(packages)
  end),

  open_log = wrap_api("dep.open_log", function()
    vim.cmd("sp " .. logger.path)
  end),

  open_config = wrap_api("dep.open_config", function()
    vim.cmd("sp " .. config_path)
  end),
}, {
  __call = function(self, config)
    config_path = debug.getinfo(2, "S").source:sub(2)
    initialized, err = pcall(function()
      base_dir = config.base_dir or (vim.fn.stdpath("data") .. "/site/pack/deps/opt/")
      packages, package_roots = {}, {}

      register("chiyadev/dep")
      register_recursive(config)
      sort_dependencies()
      ensure_acyclic()
      find_roots()
      reload_all()

      local should_sync = function(package)
        if config.sync == "new" or config.sync == nil then
          return not package.exists
        else
          return config.sync == "always"
        end
      end

      local targets = {}

      for i = 1, #packages do
        local package = packages[i]
        if should_sync(package) then
          targets[#targets + 1] = package
        end
      end

      sync_list(targets)
    end)

    if not initialized then
      logger:log("error", err)
    end
  end,
})