DEV: Remove ember-addons (#9559)

DEV: Remove ember-addons (#9559)

We weren’t using this very much and introduces a dependency between discourse-common and discourse which makes moving to yarn workspaces more difficult.

In the future we might user ember-addons properly but for now it’s easier to move the code into discourse-common.

Note the old folder is still there because at least one plugin was still requiring the old files. It will be removed in the future.

diff --git a/.git-blame-ignore-revs b/.git-blame-ignore-revs
index a4d8022..81d5a2f 100644
--- a/.git-blame-ignore-revs
+++ b/.git-blame-ignore-revs
@@ -26,9 +26,6 @@ acc5cbdf8ecb9293a0fa9474ee73baf499c02428
 # Rename discourse-common es6 -> js
 167503ca4824e37a2e93d74b3f50271556d0ba8e
 
-# Rename ember-addons es6 -> js
-16ba50bce362c1eefe1881f86c67bec66f493abb
-
 # Rename some root files
 11938d58d4b1bea1ff43306450da7b24f05db0a
 
diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js
index 67722ed..86d34b4 100644
--- a/app/assets/javascripts/application.js
+++ b/app/assets/javascripts/application.js
@@ -1,8 +1,4 @@
-//= require_tree ./ember-addons/utils
 //= require_tree ./discourse-common
-//= require ./ember-addons/decorator-alias
-//= require ./ember-addons/macro-alias
-//= require ./ember-addons/fmt
 //= require ./polyfills
 //= require_tree ./select-kit
 //= require ./discourse/app/app
diff --git a/app/assets/javascripts/discourse-common/utils/decorator-alias.js b/app/assets/javascripts/discourse-common/utils/decorator-alias.js
new file mode 100644
index 0000000..c483a1c
--- /dev/null
+++ b/app/assets/javascripts/discourse-common/utils/decorator-alias.js
@@ -0,0 +1,22 @@
+import extractValue from "discourse-common/utils/extract-value";
+
+export default function decoratorAlias(fn, errorMessage) {
+  return function(...params) {
+    // determine if user called as @discourseComputed('blah', 'blah') or @discourseComputed
+    if (params.length === 0) {
+      throw new Error(errorMessage);
+    } else {
+      return function(target, key, desc) {
+        return {
+          enumerable: desc.enumerable,
+          configurable: desc.configurable,
+          writable: desc.writable,
+          initializer: function() {
+            var value = extractValue(desc);
+            return fn.apply(null, params.concat(value));
+          }
+        };
+      };
+    }
+  };
+}
diff --git a/app/assets/javascripts/discourse-common/utils/decorators.js b/app/assets/javascripts/discourse-common/utils/decorators.js
index 36e537a..2b6548e 100644
--- a/app/assets/javascripts/discourse-common/utils/decorators.js
+++ b/app/assets/javascripts/discourse-common/utils/decorators.js
@@ -1,6 +1,8 @@
-import handleDescriptor from "ember-addons/utils/handle-descriptor";
-import isDescriptor from "ember-addons/utils/is-descriptor";
-import extractValue from "ember-addons/utils/extract-value";
+import handleDescriptor from "discourse-common/utils/handle-descriptor";
+import isDescriptor from "discourse-common/utils/is-descriptor";
+import extractValue from "discourse-common/utils/extract-value";
+import decoratorAlias from "discourse-common/utils/decorator-alias";
+import macroAlias from "discourse-common/utils/macro-alias";
 import { schedule, next } from "@ember/runloop";
 
 export default function discourseComputedDecorator(...params) {
@@ -39,8 +41,6 @@ export function readOnly(target, name, desc) {
   };
 }
 
-import decoratorAlias from "ember-addons/decorator-alias";
-
 /* eslint-disable */
 export var on = decoratorAlias(Ember.on, "Can not `on` without event names");
 export var observes = decoratorAlias(
@@ -48,8 +48,6 @@ export var observes = decoratorAlias(
   "Can not `observe` without property names"
 );
 
-import macroAlias from "ember-addons/macro-alias";
-
 export var alias = macroAlias(Ember.computed.alias);
 export var and = macroAlias(Ember.computed.and);
 export var bool = macroAlias(Ember.computed.bool);
diff --git a/app/assets/javascripts/discourse-common/utils/extract-value.js b/app/assets/javascripts/discourse-common/utils/extract-value.js
new file mode 100644
index 0000000..839d2e9
--- /dev/null
+++ b/app/assets/javascripts/discourse-common/utils/extract-value.js
@@ -0,0 +1,5 @@
+export default function extractValue(desc) {
+  return (
+    desc.value || (typeof desc.initializer === "function" && desc.initializer())
+  );
+}
diff --git a/app/assets/javascripts/discourse-common/utils/handle-descriptor.js b/app/assets/javascripts/discourse-common/utils/handle-descriptor.js
new file mode 100644
index 0000000..e4899c9
--- /dev/null
+++ b/app/assets/javascripts/discourse-common/utils/handle-descriptor.js
@@ -0,0 +1,71 @@
+import { computed, get } from "@ember/object";
+import extractValue from "./extract-value";
+
+export default function handleDescriptor(target, key, desc, params = []) {
+  return {
+    enumerable: desc.enumerable,
+    configurable: desc.configurable,
+    writeable: desc.writeable,
+    initializer: function() {
+      let computedDescriptor;
+
+      if (desc.writable) {
+        var val = extractValue(desc);
+        if (typeof val === "object") {
+          let value = {};
+          if (val.get) {
+            value.get = callUserSuppliedGet(params, val.get);
+          }
+          if (val.set) {
+            value.set = callUserSuppliedSet(params, val.set);
+          }
+          computedDescriptor = value;
+        } else {
+          computedDescriptor = callUserSuppliedGet(params, val);
+        }
+      } else {
+        throw new Error(
+          "ember-computed-decorators does not support using getters and setters"
+        );
+      }
+
+      return computed.apply(null, params.concat(computedDescriptor));
+    }
+  };
+}
+
+function niceAttr(attr) {
+  const parts = attr.split(".");
+  let i;
+
+  for (i = 0; i < parts.length; i++) {
+    if (
+      parts[i] === "@each" ||
+      parts[i] === "[]" ||
+      parts[i].indexOf("{") !== -1
+    ) {
+      break;
+    }
+  }
+
+  return parts.slice(0, i).join(".");
+}
+
+function callUserSuppliedGet(params, func) {
+  params = params.map(niceAttr);
+  return function() {
+    let paramValues = params.map(p => get(this, p));
+
+    return func.apply(this, paramValues);
+  };
+}
+
+function callUserSuppliedSet(params, func) {
+  params = params.map(niceAttr);
+  return function(key, value) {
+    let paramValues = params.map(p => get(this, p));
+    paramValues.unshift(value);
+
+    return func.apply(this, paramValues);
+  };
+}
diff --git a/app/assets/javascripts/discourse-common/utils/is-descriptor.js b/app/assets/javascripts/discourse-common/utils/is-descriptor.js
new file mode 100644
index 0000000..2e64777
--- /dev/null
+++ b/app/assets/javascripts/discourse-common/utils/is-descriptor.js
@@ -0,0 +1,9 @@
+export default function isDescriptor(item) {
+  return (
+    item &&
+    typeof item === "object" &&
+    "writable" in item &&
+    "enumerable" in item &&
+    "configurable" in item
+  );
+}
diff --git a/app/assets/javascripts/discourse-common/utils/macro-alias.js b/app/assets/javascripts/discourse-common/utils/macro-alias.js
new file mode 100644
index 0000000..5d6223f
--- /dev/null
+++ b/app/assets/javascripts/discourse-common/utils/macro-alias.js
@@ -0,0 +1,24 @@
+import isDescriptor from "discourse-common/utils/is-descriptor";
+
+function handleDescriptor(target, property, desc, fn, params = []) {
+  return {
+    enumerable: desc.enumerable,
+    configurable: desc.configurable,
+    writable: desc.writable,
+    initializer: function() {
+      return fn(...params);
+    }
+  };
+}
+
+export default function macroAlias(fn) {
+  return function(...params) {
+    if (isDescriptor(params[params.length - 1])) {
+      return handleDescriptor(...params, fn);
+    } else {
+      return function(target, property, desc) {
+        return handleDescriptor(target, property, desc, fn, params);
+      };
+    }
+  };
+}
diff --git a/app/assets/javascripts/discourse/app/lib/computed.js b/app/assets/javascripts/discourse/app/lib/computed.js
index e238320..321ea32 100644
--- a/app/assets/javascripts/discourse/app/lib/computed.js
+++ b/app/assets/javascripts/discourse/app/lib/computed.js
@@ -1,7 +1,31 @@
 import { computed } from "@ember/object";
-import addonFmt from "ember-addons/fmt";
 import { htmlSafe as htmlSafeTemplateHelper } from "@ember/template";
 
+function addonFmt(str, formats) {
+  let cachedFormats = formats;
+

[... diff too long, it was truncated ...]

GitHub sha: c1cc2f2a

This commit appears in #9559 which was approved by CvX and jjaffeux. It was merged by eviltrout.